Ben
Ben

Reputation: 5777

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

Upvotes: 5

Views: 74

Answers (3)

Amit
Amit

Reputation: 46361

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));

Upvotes: 3

Giovanni Giordano
Giovanni Giordano

Reputation: 37

Try to change dt.toUTCString() in another function. There are a lot of hour on the planet,for example in America is the 5 o' Clock,in Japan is 10 o' Clock etc... the UTC is a time zone,try to change this.

Upvotes: 0

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28475

The toUTCString() method converts a Date object to a string, according to universal time.

The Universal Coordinated Time (UTC) is the time set by the World Time Standard.

Note: UTC time is the same as GMT time.

Upvotes: 0

Related Questions