获取UTC日期按日期和时区名称

发布于 2025-01-26 04:47:41 字数 317 浏览 2 评论 0原文

我需要一个功能来获得UTC日期时间。我仅通过日期,时间和时区名称。我需要获取UTC时间和日期。谢谢你

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2025-02-02 04:47:41

我将使用一个库,例如 day.js 为此目的。您可以在时区中的日期和时间分析,转换为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("YYYY-MM-DD[T]HH:mm:ss.SSS[Z]");
  return utcDate;
}

I would use a library like Day.js for this purpose. You can parse in the date and time in a timezone, convert to UTC, then display in the desired format.

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("YYYY-MM-DD[T]HH:mm:ss.SSS[Z]");
  return utcDate;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文