简体   繁体   中英

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?

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.

Kindly try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM