新的Date()转换时区 [英] new Date() converts timezone

查看:82
本文介绍了新的Date()转换时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 new Date()转换时区?我希望我的日期与我提供的字符串相同,所以 00:30 而不是 10:30 .

Why is new Date() converting the timezone? I'd like my date to be the same as the string I provide, so 00:30 and not 10:30.

>>> new Date("2015-04-11T00:30:00");
Sat Apr 11 2015 10:30:00 GMT+1000 (AEST)

推荐答案

您已将ISO格式的日期传递给了构造函数"2015-04-11T00:30:00".这意味着您的浏览器不将其解释为本地时间,而是将其解释为UTC.但是Date.toString使用您的当地时间.如果要使用UTC时间,请致电.toUTCString或更好的.toISOString.

You passed the date in ISO form into the constructor "2015-04-11T00:30:00". That means your browser interprets that not as local time but as UTC. Date.toString however uses your local time. If you want to use UTC time call .toUTCString or better yet .toISOString.

这篇关于新的Date()转换时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆