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
The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
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 .
getDate() The getDate() method returns the day of the month for the specified date according to local time.
new Date(1970, 1, 1) actually is Feb. Months are zero-indexed. Try changing it to new Date(1970, 0, 1).
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
: )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With