Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date javascript

I am trying to understand more about the Date object in javascript. I thought that when you call valueOf(), you get the amount of milliseconds since january 1, 1970. So what I would expect is that the following should return exactly zero;

alert((new Date(1970, 1, 1).valueOf() )/ ( 86400 * 1000));

but it does not, it returns 30.958333333333332. What am I missing?

gr,

Coen

like image 916
Coen Avatar asked Sep 01 '10 13:09

Coen


People also ask

What is date now () in JavaScript?

The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

What date format is dd mm yyyy in JavaScript?

To format a date as dd/mm/yyyy:Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 .

How does JavaScript date?

getDate() The getDate() method returns the day of the month for the specified date according to local time.


2 Answers

new Date(1970, 1, 1) actually is Feb. Months are zero-indexed. Try changing it to new Date(1970, 0, 1).

like image 97
svanryckeghem Avatar answered Oct 14 '22 07:10

svanryckeghem


Second parameter, month, starts with 0, so you need to do:

alert((new Date(1970, 0, 1).valueOf() )/ ( 86400 * 1000));

but even with this you'll get the offset, in seconds, off GMT.

the value you posted says you are GMT +1 : )

like image 2
aularon Avatar answered Oct 14 '22 07:10

aularon