0

I have this kind of problem. I want to set a date in UTC format given the date element. The date is the 31 october 2017 UTC time If I do this:

 var d1 = new Date(); d1.setUTCMilliseconds(0); d1.setUTCSeconds(0); d1.setUTCMinutes(0); d1.setUTCHours(0); d1.setUTCDate(31); d1.setUTCMonth(9); //9 is october d1.setUTCFullYear(2017); console.log(d1); 

The date printed in the console is: 2017-10-01T00:00:00.000Z it is the first of october. All the other days work as expected, but not the last day of the month. Is this a bug or is there something I don't know? I'm just a beginner with Javascript. Thank you

1 Answer 1

1

The problem is the order in which you're setting the fields. For each field, the Date object tries to make your number work. Since most months don't have 31 days, whatever month the Date has at that moment determines what it does with that 31 value. For instance, as I write this it's November, so new Date gives us a date in November. Calling setDate(31) will set the date to December 1st, because the Date object tries to make 31 work even though November has only 30 days. If it were currently February in a non-leap year, setDate(31) would set the date to March 3rd.

Instead, use new Date with Date.UTC:

var d1 = new Date(Date.UTC(2017, 9, 31)); // All the others will default to 0 

Live example:

// Your way var d1 = new Date(); d1.setUTCMilliseconds(0); d1.setUTCSeconds(0); d1.setUTCMinutes(0); d1.setUTCHours(0); d1.setUTCDate(31); d1.setUTCMonth(9); //9 is october d1.setUTCFullYear(2017); console.log("Your way:"); console.log(d1.toISOString()); // Using new Date(Date.UTC(...)) instead: d1 = new Date(Date.UTC(2017, 9, 31)); console.log("Using new Date(Date.UTC(...)) instead:"); console.log(d1.toISOString());

If you had to do it with individual calls for some reason, you'd want to set the day-of-month to 1 (since, as you say in a comment, if it just happened to be 31, setting the month to November would result in Dec 1st!), and then set the fields in order largest unit to smallest unit: Year, month, day, hour, minute, second, ms:

d1 = new Date(); d1.setUTCDate(1); d1.setUTCFullYear(2017); d1.setUTCMonth(9); //9 is october // ...and so on in order largest unit to smallest unit 

Live example:

// Setting them in order: Year, month, day, hour, minute, second, ms var d1 = new Date(); d1.setUTCDate(1); d1.setUTCFullYear(2017); d1.setUTCMonth(9); //9 is october d1.setUTCDate(31); d1.setUTCHours(0); d1.setUTCMinutes(0); d1.setUTCSeconds(0); d1.setUTCMilliseconds(0); console.log("Setting them in order: Year, month, day, hour, minute, second, ms:"); console.log(d1.toISOString());

Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work also with largest to smallest, indeed it was in that way, but it was the 31 of october 2017, and set the date as 01 of november, it results as the 1st of december, changing the order has solved that problem, but it generates the problem described here. The Date.UTC wasn't supported on iOS, at least years ago, perhaps now it works. Thank you
@Davide: Going largest to smallest absolutely does work. Re Date.UTC: It's been in the ECMAScript specification since the 1st edition, 1997. I very, very much doubt any JavaScript engine on iOS failed to support it, as the first release of iOS was 10 years after that.
Try this code: var d1 = new Date("2017-10-31"); console.log(d1); d1.setUTCFullYear(2017); d1.setUTCMonth(10); //10 is november d1.setUTCDate(1); d1.setUTCHours(0); d1.setUTCMinutes(0); d1.setUTCSeconds(0); d1.setUTCMilliseconds(0); console.log(d1); ---- On my system it gives me 2017-12-01 instead of 2017-11-01, if the starting date is 2017-10-31
But, as per you reply, I can understand why it is so. I'm setting the month to november, but the day is still 31, but november has 30, so it switch to december....
@Davide: You're absolutely right, how silly of me. I've fixed the answer, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.