I’m attempting to use the dayjs lib to parse incoming ISO strings. I need the output to be in UTC time, not converted to my machine’s local timezone.
This is my rough draft code:
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
dayjs.extend(utc);
const formats = ["YYYY-MM-DDTHH:mm:ss[Z]", "YYYY-MM-DD"];
const validateDate = (date) => {
if (!date.isValid()) {
throw new Error(`Invalid date format given (${date})`);
}
};
exports.parseISOString = (dateString) => {
const parsedDate = dayjs.utc(dateString, formats, false);
console.dir(parsedDate.utc().format("YYYY-MM-DD HH:mm:ss"))
validateDate(parsedDate);
return parsedDate.toDate();
};
Here is the output from some tests I’ve written beforehand. The green is the console.dir
from the function, and the red text contains the ISO string passed into the function.
As you can see, dayjs appears to be converting the dates to my machine’s local time (NZT), despite the fact that I’m using the utc plugin. Is there something I’m doing wrong for this to be happening?