At my new workplace, they represent a lot of dates as "days since epoch" (which I will hereafter call DSE). I'm running into issues in JavaScript converting from DSE to seconds since epoch (UNIX timestamps). Here's my function to do the conversion:
在我的新工作场所,他们代表了许多日期为“自纪元以来的日子”(我将在此后称之为DSE)。自从epoch(UNIX时间戳)以来,我遇到了从DSE转换为秒的问题。这是我进行转换的功能:
function daysToTimestamp(days) {
return Math.round(+days * 86400);
}
By way of example, when I pass in 13878 (expecting that this represents January 1, 2008), I get back 1199059200, not 1199098800 as I expect. Why?
举例来说,当我通过13878(期望这代表2008年1月1日)时,我得到了1199059200,而不是我预期的1199098800。为什么?
5 个解决方案
#1
4
1199059200 represents December 31 2007 in UTC. Sample Python session:
1199059200表示2007年12月31日UTC。示例Python会话:
>>> import time
>>> time.gmtime(1199059200)
(2007, 12, 31, 0, 0, 0, 0, 365, 0)
Remember that all time_t
values are against UTC. :-) You have to adjust accordingly to your timezone.
请记住,所有time_t值都是针对UTC的。 :-)你必须根据你的时区进行调整。
Edit: Since you and I are both in New Zealand, here's how you might have got the 1199098800 value:
编辑:既然你和我都在新西兰,你可能会得到1199098800的价值:
>>> time.localtime(1199098800)
(2008, 1, 1, 0, 0, 0, 1, 1, 1)
This is so because in New Year (summer in New Zealand), the timezone here is +1300. Do the maths and see. :-)
这是因为在新年(新西兰的夏天),这里的时区是+1300。做数学并看看。 :-)
For January 1 2008 in UTC, add 86400 to 1199059200, and get 1199145600.
对于2008年1月1日的UTC,添加86400到1199059200,得到1199145600。
>>> time.gmtime(1199145600)
(2008, 1, 1, 0, 0, 0, 1, 1, 0)
#2
3
It is because it is neither a linear representation of time nor a true representation of UTC (though it is frequently mistaken for both) as the times it represents are UTC but it has no way of representing UTC leap seconds
这是因为它既不是时间的线性表示也不是UTC的真实表示(虽然它经常被误认为是两者),因为它表示的时间是UTC,但它无法表示UTC闰秒
#3
1
Unix times (time_t) are represented in seconds since Jan 1, 1970 not milliseconds.
Unix时间(time_t)以1970年1月1日以秒为单位表示,而不是毫秒。
I imagine what you are seeing is a difference in timezone. The delta you have is 11 hours, how are you getting the expected value?
我想你所看到的是时区的差异。您拥有的delta是11个小时,您是如何获得预期值的?
#4
1
Because 1199098800/86400 = 13878.4583333333333 (with the 3 repeating forever), not 13878.0. It gets rounded to 13878.0 since it's being stored as an integer. If you want to see the difference it makes, try this: .4583333333333*86400 = 39599.99999999712. Even that makes it slightly incorrect, but this is where the discrepancy comes from, as 1199098800-1199059200=35600.
因为1199098800/86400 = 13878.4583333333333(与3重复永远),而不是13878.0。它被舍入到13878.0,因为它被存储为整数。如果你想看到它的差异,试试这个:.4583333333333 * 86400 = 39599.99999999712。即使这样也会略微不正确,但这就是差异所在,如1199098800-1199059200 = 35600。
#5
-1
You should multiply by 86400000
你应该乘以86400000
1 day = 24 hours * 60 minutes * 60 seconds * 1000 milliseconds = 86400000
1天= 24小时* 60分钟* 60秒* 1000毫秒= 86400000
#1
4
1199059200 represents December 31 2007 in UTC. Sample Python session:
1199059200表示2007年12月31日UTC。示例Python会话:
>>> import time
>>> time.gmtime(1199059200)
(2007, 12, 31, 0, 0, 0, 0, 365, 0)
Remember that all time_t
values are against UTC. :-) You have to adjust accordingly to your timezone.
请记住,所有time_t值都是针对UTC的。 :-)你必须根据你的时区进行调整。
Edit: Since you and I are both in New Zealand, here's how you might have got the 1199098800 value:
编辑:既然你和我都在新西兰,你可能会得到1199098800的价值:
>>> time.localtime(1199098800)
(2008, 1, 1, 0, 0, 0, 1, 1, 1)
This is so because in New Year (summer in New Zealand), the timezone here is +1300. Do the maths and see. :-)
这是因为在新年(新西兰的夏天),这里的时区是+1300。做数学并看看。 :-)
For January 1 2008 in UTC, add 86400 to 1199059200, and get 1199145600.
对于2008年1月1日的UTC,添加86400到1199059200,得到1199145600。
>>> time.gmtime(1199145600)
(2008, 1, 1, 0, 0, 0, 1, 1, 0)
#2
3
It is because it is neither a linear representation of time nor a true representation of UTC (though it is frequently mistaken for both) as the times it represents are UTC but it has no way of representing UTC leap seconds
这是因为它既不是时间的线性表示也不是UTC的真实表示(虽然它经常被误认为是两者),因为它表示的时间是UTC,但它无法表示UTC闰秒
#3
1
Unix times (time_t) are represented in seconds since Jan 1, 1970 not milliseconds.
Unix时间(time_t)以1970年1月1日以秒为单位表示,而不是毫秒。
I imagine what you are seeing is a difference in timezone. The delta you have is 11 hours, how are you getting the expected value?
我想你所看到的是时区的差异。您拥有的delta是11个小时,您是如何获得预期值的?
#4
1
Because 1199098800/86400 = 13878.4583333333333 (with the 3 repeating forever), not 13878.0. It gets rounded to 13878.0 since it's being stored as an integer. If you want to see the difference it makes, try this: .4583333333333*86400 = 39599.99999999712. Even that makes it slightly incorrect, but this is where the discrepancy comes from, as 1199098800-1199059200=35600.
因为1199098800/86400 = 13878.4583333333333(与3重复永远),而不是13878.0。它被舍入到13878.0,因为它被存储为整数。如果你想看到它的差异,试试这个:.4583333333333 * 86400 = 39599.99999999712。即使这样也会略微不正确,但这就是差异所在,如1199098800-1199059200 = 35600。
#5
-1
You should multiply by 86400000
你应该乘以86400000
1 day = 24 hours * 60 minutes * 60 seconds * 1000 milliseconds = 86400000
1天= 24小时* 60分钟* 60秒* 1000毫秒= 86400000