I need a function to get UTC date-time. I passed only the date, time and time zone name only. I need to get the UTC time and date. Thank You
Ex:
const utcDate = getUTCDateTime("2022-05-10","18:00" "America/Hermosillo");
// OutPut should be like this ==>> 2022-05-11T01:00:00.527Z
function getUTCDateTime(date,time, timeZoneName){
//Logic
return utcDate;
}
CodePudding user response:
You should try and take a look at this forum. There have been many answers relating to your question. How can I get the timezone name in JavaScript?
CodePudding user response:
I would use a library like Day.js for this purpose. You can parse the date and time in a timezone then format as UTC.
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(customParseFormat);
function getUTCDateTime(date, time, timeZoneName){
const utcDate = dayjs.tz(`${date} ${time}`, "YYYY-MM-DD HH:mm", timeZoneName).utc().format();
return utcDate;
}
Have not tested yet but you can play around and achieve what you need with this library