JavaScript日期中“01”和“1”之间的差异 [英] Difference between '01' and '1' in a JavaScript date

查看:131
本文介绍了JavaScript日期中“01”和“1”之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中2015-10-01'和'2015-10-1'的日期有什么区别?

What is the difference between the dates '2015-10-01' and '2015-10-1' in JavaScript?

new Date('2015-10-1')

返回 '2015年10月1日00:00:00 GMT-0300'

new Date('2015-10-01')

返回'Wed Sep 30 2015 21:00 :00 GMT-0300'

推荐答案

我在本地执行后看到的是

What I see after executing locally is


  • 日期当地时区

  • 日期 UTC

  • Date in local timezone
  • Date in UTC

根据 MDN文档 Date.parse 如果日期完整,则假定日期为UTC格式 DD 否则它将采用当地时区格式。

As per the MDN docs, Date.parse will assume the date to be UTC format if it has complete DD else it will assume in local timezone format.

关于月昌的详细解释e:(评论)

新日期('2015-10-1')当你执行此语句输出'Thu Oct 01 2015 00:00:00 GMT-0300'。即它是你当地时间,它是格林威治标准时间-3小时。

new Date('2015-10-1') when you execute this statement output is 'Thu Oct 01 2015 00:00:00 GMT-0300'. i.e. its your local time and it is GMT -3hrs.

但当你执行新日期('2015-10-01')输出为'Wed Sep 30 2015 21:00:00 GMT-0300',这是UTC时间。即3小时减去您当地时间。

But when you execute the new Date('2015-10-01') the output is 'Wed Sep 30 2015 21:00:00 GMT-0300' which is in UTC time. i.e. 3hrs minus from your local time.

所以它是 10月1日午夜-3小时,这是前一天的21小时。即9月30日21点。

So it is 1st Oct midnight -3hrs, which is previous day's 21hrs. i.e. Sep 30 21hrs.

这篇关于JavaScript日期中“01”和“1”之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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