I have an object with time information.
const dateTime = {
day: 3,
hour: 18,
minute: 22,
month: 11,
second: 54,
timeZoneOffset: -60,
year: 2022
}
How do I convert this object to ISO UTC time format?
I have an object with time information.
const dateTime = {
day: 3,
hour: 18,
minute: 22,
month: 11,
second: 54,
timeZoneOffset: -60,
year: 2022
}
How do I convert this object to ISO UTC time format?
The Date
API allows to build a date object with all your arguments.
You'd have to create a new Date object with your value, and then you can convert your object to the format you want (UTC ISO string...).
If the timezone is not the timezone of the locale machine, you'd have to workaround with the setTime
method.
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Also see this answer for more info about setting time zone
const dateTime = {
day: 3,
hour: 18,
minute: 22,
month: 11,
second: 54,
timeZoneOffset: -60,
year: 2022
};
const {
day,
hour,
minute,
month,
second,
year,
timeZoneOffset
} = dateTime;
const d = new Date(Date.UTC(year, month, day, hour, minute, second));
d.setTime(d.getTime() + timeZoneOffset * 60000);
console.log(d.toISOString());
console.log(d.toUTCString());