简体   繁体   English

javascript - 日期差异应为零但是18小时

[英]javascript - date difference should be zero but it is 18 hours

This one has stumped me. 这个让我难过。 It should be so simple I would think. 它应该是如此简单我想。 I am doing some very simple date subtraction in Javascript. 我在Javascript中做一些非常简单的日期减法。 I am subtracting the same dates and I would think it would give zero hours, but it gives 18 hours. 我减去相同的日期,我认为它会给零时,但它给了18个小时。

 let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime(); let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime(); document.getElementById('date').innerHTML = new Date(outDate - inDate); 
 <div id='date'> </div> 

In case it produces different results based on where you are, the result I am getting is this: 如果它根据你的位置产生不同的结果,我得到的结果是:

Wed Dec 31 1969 18:00:00 GMT-0600 (Central Standard Time)

This is due to your timezone . 这是由于您的timezone If you convert to GMT String before print it the time will be correct. 如果在打印之前转换为GMT String ,则时间将是正确的。 (Jan 01, 1969 00:00:00) (1969年1月1日00:00:00)

 new Date(outDate - inDate).toGMTString()

You should see the correct date. 你应该看到正确的日期。

 let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime() let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime() console.log(new Date(inDate - outDate).toGMTString()) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM