How can i set the date to the beginning of calendar

Viewed 67

I want to set the time to the 1st January 0000 and time at 00:00:00 i tried to calculate a lot but i'm unable to set the date to the beginning point.

I am setting the date with negative sign like this -

new Date(-4000) but setting the year to 0 gives me only the 1900 so how can get to the perfect date of 1st Jan 0000 and time at 00:00:00.

2 Answers

Try this

function d() {
let d = new Date('1  January 0000, 00:00:00');
alert(d);
}
<input type=button onClick="d()" value="Click">

new Date(0) // Thu Jan 01 1970 05:30:00

This can be seen as the reference against which all the dates or times are calculated in Javascript. Now you need to find the time difference in milliseconds from the reference to the time you have mentioned.

This could be calculated, and one would end up with a huge number doing this. The best way I can recommend is to use the API to set Date and Time at what one desires. This ensures a more readable and efficient code that calculating this offset or storing it in your code.

The code below should work without the need for a huge magic number. The best part even the time is set to what was mentioned in the question.

let now = new Date();
now.setHour(0);
now.setMinute(0);
now.setSeconds(0);
now.setMilliseconds(0);
now.setFullYear(0,0,1);

let ad0 = new Date(now);
console.log(ad0); // Jan 01 0000 00:00:00
Related