Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain javascripts Date() functions

Why is it when I have

var dt = new Date(2015, 6, 1);
dt.toUTCString()

My output is Tue, 30 Jun 2015 23:00:00 GMT

And

var dt = new Date(2015, 6, 2);
dt.toUTCString()

Wed, 01 Jul 2015 23:00:00 GMT

I'm clearly missing something here, I want to be able to loop through each days of the month and get a Date() for that day

I don't understand why if the day is 1, it says the date is the 30th

like image 888
Ben Avatar asked Oct 20 '22 10:10

Ben


1 Answers

Javascript dates are always generated with local time zone. Using toUTCString converts the time in the Date object to UTC time, and apparently in your case that means -1 hours. If you want to initialize a Date object with UTC time, use:

var dt = new Date(Date.UTC(2015, 6, 1));
like image 75
Amit Avatar answered Oct 21 '22 23:10

Amit