5

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

1
  • This is a good question, clear, useful and indeed interesting. Commented Jun 6, 2015 at 17:18

3 Answers 3

3

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));
Sign up to request clarification or add additional context in comments.

1 Comment

I think I'm just being stupid. I thought toUTCString() was the only function to view the readable date, just realized there's a toDateString() and that brings up the right date
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.