learnerplates
learnerplates

Reputation: 4387

How does minus integer work in Date?

How does javascript Date interpret the milisecond integers?

var d = new Date(-1724115600000); //this gives me a date in the past, which I want
console.log(d);

var d = new Date(1724115600000); 
console.log(d);

(we had a bug where the - sign was not getting through. But I dont understand the significance of the -)

Upvotes: 0

Views: 70

Answers (3)

Karthik
Karthik

Reputation: 114

//negative sign give you the date before 1970. in your example

var d = new Date(-1425223942000);// this gives date in the past
        document.write(d)  //Sun Nov 02 1924 03:27:38 GMT-0500 (Eastern Standard Time)

document.write('<br/>')
var d = new Date(1425223942000); //This gives date in th  future. 
document.write(d); // Sun Mar 01 2015 10:32:22 GMT-0500 (Eastern Standard Time)

//Unfortunately i cannot post the screenshots yet

Upvotes: 1

user1693593
user1693593

Reputation:

0 would be 1. January 1970. The delta is given as an unsigned number representing milliseconds. If you want dates before that you need to use negative values in milliseconds.

The negative number you provided will give a number in the past, the other one in the future:

Date 1915-05-14T23:00:00.000Z
Date 2024-08-20T01:00:00.000Z

If you got one in the past with the second number it may have been missing the last digit when your tried. In that case it would give:

Date 1975-06-19T12:06:00.000Z

var d = new Date(-1724115600000); //this gives me a date in the past, which I want
document.write(d + "<br>");

var d = new Date(1724115600000); //This gives me a date in the past too. 
document.write(d + "<br>");

var d = new Date(172411560000); //missing last digit
document.write(d);

Upvotes: 2

James Thorpe
James Thorpe

Reputation: 32212

The Date object constructor can take a variety of inputs, but when called in this fashion it's using the integer value one:

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).

Negative values will give dates before the Unix Epoch, positive values are dates after the Epoch.

Upvotes: 2

Related Questions