Skip to content Skip to sidebar Skip to footer

Difference Between New Date(...).gettime() And Date.utc(...)

Can someone explain why this two commands returns different results: > new Date(2019, 0, 1).getTime() > 1546293600000 > Date.UTC(2019, 0, 1) > 1546300800000 According

Solution 1:

new Date(2019, 0, 1) creates a Date object representing the start of January, 1, 2019 in your locale specificed timezone. When converted to UTC, it will be a different time than the start of Jan 1 in UTC.

Running your second snippet to add one year gives the correct result for me. What browser or context are you running in?

Solution 2:

new Date(2019, 0, 1) makes an object from your timezone, whereas Date.UTC(2019, 0, 1) gives you the timestamp in UTC.

I am in CE(S)T, so there will be a difference of 1 hour, 3600000 milliseconds:

new Date(2019, 0, 1).getTime() // time in CEST
> 1546297200000
Date.UTC(2019, 0, 1) // time in UTC
> 1546300800000
new Date(2019, 0, 1).getTime() - Date.UTC(2019, 0, 1) // Subtract my time from UTC (I'm ahead, so the difference will be negative)
> -3600000

Post a Comment for "Difference Between New Date(...).gettime() And Date.utc(...)"