Skip to content
Advertisement

How to convert year month day to proper UTC milliseconds from the epoch?

I am trying to convert a date into milliseconds with the following code:

JavaScript

I get left=-2206310400000, but when I check here, I should get -2208988800000.

What am I doing wrong?

Advertisement

Answer

You’re using 1 for the month number, which means February.

You mean

JavaScript

From the docs:

month – the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.

Yes, the Java date/time API is broken. If you’re doing any significant amount of work in dates/times, I’d suggest you use Joda Time instead.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement