0

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?

user3740359
  • 395
  • 1
  • 6
  • 16
  • `var newdate = d.setUTCMonth(1); console.log(newdate.toISOString());` – GrafiCode Mar 29 '19 at 10:36
  • 1
    dates "auto correct" in javascript ... clearly there is no 29th feb 2019, so 1 mar 2019 is correct ... try date.setDate(date.getDate() + 365) for any date, you'll add (3 in 4 times, damn yuo leap years) exactly a year .. – Jaromanda X Mar 29 '19 at 11:15
  • 1
    It will also happen on the 31st of any moth that follows a 30 day month. ;-) – RobG Mar 29 '19 at 13:46

2 Answers2

1

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.

Francisco
  • 2,018
  • 2
  • 16
  • 16
0

Kindly try this:

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