user3740359
user3740359

Reputation: 405

Javascript date - setting UTC month

Today i ran my automates tests and wondered why I got an error about some date specific stuff.

Turns out that setting a fixed UTC month is not working anymore. But yesterday it did. There are no changes I am aware of.

I tried running the following code

var d = new Date();
d.setUTCMonth(1);
d.toISOString();

which returns

"2019-03-01T10:28:42.108Z"

But month should obviously be february. Also why is the day set to 01 and not today (29)

Tested on Chrome, Edge, Firefox.

Any advice? Am I doing something wrong? Is there a bug in the library?

Upvotes: 0

Views: 130

Answers (2)

Saket Yadav
Saket Yadav

Reputation: 1017

Kindly try this:

var d = new Date('2019-03-10T00:00:00');
d.setUTCMonth(1); 
console.log(d.toISOString());

Upvotes: 0

Francisco
Francisco

Reputation: 2056

hum... funny error.

Try this :

var d = new Date('2019-03-10T00:00:00');
d.setUTCMonth(1);
d.toISOString();

It's because today is end of month and you're initializing the date to today.

Upvotes: 1

Related Questions