最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Date.getTime() gives two different values for same timestamp - Stack Overflow

programmeradmin1浏览0评论

I have two variables. weekStartDate and startDate. they both hold essentially the same timestamp:

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)}

the problem is: when I try to getTime(), they show a slightly different value:

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

how can i fix this?

I have two variables. weekStartDate and startDate. they both hold essentially the same timestamp:

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)}

the problem is: when I try to getTime(), they show a slightly different value:

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

how can i fix this?

Share Improve this question edited Mar 30, 2012 at 14:15 Th0rndike 3,4363 gold badges25 silver badges43 bronze badges asked Mar 30, 2012 at 14:12 amitamit 10.2k23 gold badges76 silver badges125 bronze badges 2
  • 2 User this.weekStartDate.setMilliseconds(0). – Pointy Commented Mar 30, 2012 at 14:14
  • Can you show how you're instantiating them? – Nik Commented Mar 30, 2012 at 14:18
Add a ment  | 

2 Answers 2

Reset to default 6

The difference is 506 milliseconds. The number of milliseconds isn’t displayed when you call toString() on a Date object, so anything more precise than seconds will go unnoticed unless you pare the numeric value.

To reset the milliseconds to 0, use:

this.weekStartDate.setMilliseconds(0);

I think the milliseconds are different. Set the milliseconds of boths dates to zero then the dates will be the same:

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

edit: damn i was to slow

发布评论

评论列表(0)

  1. 暂无评论