Date.getTime() 为同一时间戳提供两个不同的值

Date.getTime() gives two different values for same timestamp

本文关键字:两个 时间戳 getTime 一时间 Date      更新时间:2023-09-26

我有两个变量。 weekStartDate 和 startDate。 它们都拥有基本相同的时间戳:

this.startDate  Date {Mon Mar 26 2012 00:00:00 GMT+0530 (IST)}
this.weekStartDate   Date {Mon Mar 26 2012 00:00:00 GMT+0530 (IST)}

问题是:当我尝试 getTime(( 时,它们显示的值略有不同:

this.startDate.getTime()    1332700200000
this.weekStartDate.getTime()    1332700200506

我该如何解决这个问题?

相差 506 毫秒。在Date对象上调用toString()时,不会显示毫秒数,因此除非比较数值,否则不会注意到比秒更精确的任何内容。

要将毫秒重置为 0 ,请使用:

this.weekStartDate.setMilliseconds(0);

我认为毫秒是不同的。将两个日期的毫秒数设置为零,则日期将相同:

this.weekStartDate.setMilliseconds(0);
this.startDate.setMilliseconds(0);

编辑:该死的我要慢