JavaScript is determined to ensure I get the wrong date.
I have UTC dates from the server. If I take the first one, it looks like this: 2021-03-09 19:00:00
.
My chart takes a date object. So I want to convert from UTC. The correct answer is:
-Tue Mar 09 2021 12:00:00 GMT-0700 (Mountain Standard Time)
Here is an insane overcoded attempt to force the right answer:
// given inputDate from the server
let re: RegExp = /{regular expression parsing date}/;
let m: RegExpMatchArray = inputDate.match(re);
let yyyy: number = Number(m[1]); // 2021
let mm: number = Number(m[2]); // 3
let dd: number = Number(m[3]); // 9
let hh: number = Number(m[4]); // 19
let utc: number = Date.UTC(yyyy, mm, dd, hh);
let utcDate: Date = new Date(utc);
console.log('interp', `${yyyy}-${mm}-${dd} ${hh}:00:00`);
console.log('utcDate', utcDate.toISOString());
Result:
interp 2021-3-9 19:00:00
utcDate 2021-04-09T19:00:00.000Z
Am I going insane? Why did it add a month??
Moment works so I guess I can stomach the extra 70KB:
let tryUtc: Moment = moment(inputDate).utc(true);
console.log('local', tryUtc.toDate()); // bingo
I still want to know why the month got added.