admin管理员组

文章数量:1186459

I'm trying to write a javascript function that calculates the time since Oct 11th, 1910 so I can throw it into a timer for a project I'm working on. I get that javascript's milliseconds works from epoc, but I don't and can't find a way to get the milliseconds since a date earlier than 01.01.1970

Does anyone have any loose code that can do the above that they may be willing to share?

I'm trying to write a javascript function that calculates the time since Oct 11th, 1910 so I can throw it into a timer for a project I'm working on. I get that javascript's milliseconds works from epoc, but I don't and can't find a way to get the milliseconds since a date earlier than 01.01.1970

Does anyone have any loose code that can do the above that they may be willing to share?

Share Improve this question asked Oct 1, 2009 at 0:29 RobbyRobby 3
  • 9 Aren't negative values before Jan 1 1970? – cletus Commented Oct 1, 2009 at 0:31
  • Any date before that Unix epoq seems to give a NaN value. ex : new Date("333-02-06T06:00:00") ==> Nan This is a problem for time processing in astronomy applications for dates before 1970... – bendeg Commented Jun 13, 2022 at 13:50
  • 1 @bendeg According to spec you can go to 8.64e+15 on either side of 01 January, 1970 UTC. So if you do new Date(8.64e+15) or new Date(-8.64e+15) you'll still get valid dates. But the date parsers on v8 (Chrome, node, deno) and Firefox can only handle four digit values for year that's why your date shows Invalid Date. On jsc (Safari, bun), it actually handles all possible year values and that statement gives valid date there. – brc-dd Commented Sep 23, 2024 at 13:55
Add a comment  | 

3 Answers 3

Reset to default 21

var oldGoodTimes = new Date(1910, 9, 11); // January = 0
var actualDate = new Date();
console.log(actualDate.getTime() - oldGoodTimes.getTime());

Try this:

var yeOldeTimes = new Date();
yeOldeTimes.setFullYear(1910, 9, 11);

var myNewDate = new Date();
console.log("Milliseconds since Ye Olde Times: " + (myNewDate - yeOldeTimes));

Number of milliseconds since Oct 11th, 1910

console.log(new Date() - new Date('1910', '10', '11'))

// new Date().valueOf() - milliseconds since 1970
// -(new Date('1910', '10', '11')).valueOf() - milliseconds from 1910 since 1970

本文标签: javascriptDates before Jan 01 1970Stack Overflow