JavaScript中new Date()中显示的日期不正确 [英] Incorrect date shown in new Date() in JavaScript

查看:705
本文介绍了JavaScript中new Date()中显示的日期不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在chrome控制台中得到的.我输入了"2016-09-05"(YYYY-MM-DD)作为日期,并向我显示了2016年9月4日.

This is what I get in chrome console. I pass "2016-09-05"(YYYY-MM-DD) as the date and it shows me Sept 4,2016 as the date.

另一个构造函数显示正确的日期

Another constructor shows the right date

以逗号分隔传递它需要一些标记化+解析+使我想避免的月份为零索引

Passing it comma separated needs some tokenizing + parsing + making month zero indexed which I want to avoid

推荐答案

如果省略new Date(string)构造函数中的时间,则采用UTC时间.因此,显示的值实际上是正确的.使用new Date('2016-09-05 00:00')在本地时间创建日期对象.

If you omit the time in the new Date(string) constructor, UTC time is assumed. So the displayed value is actually correct. Use new Date('2016-09-05 00:00') to create the date object in local time.

这篇关于JavaScript中new Date()中显示的日期不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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