为什么新的Date()删除一天? - Javascript [英] Why is new Date() removing a day? - Javascript

查看:128
本文介绍了为什么新的Date()删除一天? - Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新建Date()创建日期。当我这样做,它减去一天。这是代码:

I am creating a date with new Date(). When I do this, it is subtracting a day. Here is the code:

var dateString = "2016-04-10";
var date = new Date(dateString);

// date = Sat Apr 09 2016 18:00:00 GMT-0600 (MDT)

我有什么误解?为什么日期不是 2016年4月10日

What do I misunderstand? Why is the date not Apr 10 2016? How do I make this work properly?

推荐答案

您的时区为GMT-6(由您提供的输出中的GMT-0600(MDT))。因此,生成的日期将被-6小时抵消。在这种情况下,午夜6小时是前一天的下午6点。

Your timezone is GMT-6 (as revealed by the GMT-0600 (MDT) in the output you've provided). Therefore the date which gets generated is offset by -6 hours. In this case, midnight minus 6 hours is 6PM on the previous day.

如果您调用 date.toISOString(),你会看到UTC时间是$ 0400:00.000Z

If you call date.toISOString(), you'll see that the UTC time is "2016-04-10T00:00:00.000Z" as expected.

这篇关于为什么新的Date()删除一天? - Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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