【问题标题】:Get timestamp with given date vs today date doesn't same result获取给定日期的时间戳与今天日期的结果不同
【发布时间】:2013-10-18 08:43:30
【问题描述】:

今天是 2013 年 10 月 18 日

var tmp = new Date('2013-10-18');
    tmp = tmp.getTime();

1382054400000 (格林威治标准时间:格林威治标准时间 2013 年 10 月 18 日星期五 00:00:00)

var today = new Date();
    today = today.setHours(0,0,0,0);

1382047200000 (格林威治标准时间:2013 年 10 月 17 日星期四 22:00:00 GMT)


.setHours(0,0,0,0) 不将日期设置为午夜 (00:00:00) 吗?

【问题讨论】:

  • 您在哪个时区运行该代码?您距离格林威治标准时间 +/- 2 小时吗?

标签: javascript date timestamp


【解决方案1】:

Date.setHours 将在您当前的时区中将时间设置为“00:00:00:00”。

Sets the hours for a specified date according to local time, and returns the number of milliseconds since 1 January 1970 00:00:00 UTC until the time represented by the updated Date instance.

如果您想在 UTC 时间工作,请改用 Date.setUTCHours

【讨论】:

  • 如果您希望它在 UTC 时间工作,请改用 setUTCHours,请参阅更新。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多