如何在 javascript 中获取指定时区的一天的开始时间和结束时间(以 UTC 为单位)

How to get the start time and end time in utc of a day for a specified timezone in javascript?

本文关键字:时间 结束 为单位 UTC 开始时 一天 javascript 获取 定时区 取指 开始      更新时间:2023-09-26

正如标题所述,我想获取特定时区一天的开始时间和结束时间,并将它们转换为UTC时间。这是我的实现部分:

//convert current local time to specified timezone time
var converted = moment.tz(moment(), timezone).format("YYYY-MM-DD");
var full_format = "YYYY-MM-DD HH:mm:ss";
// get start time and end time in the timezone
var start = converted + " 00:00:00";
var end = converted + " 23:59:59";
// how to convert them in utc time by momentjs or other methods? 
var utc_start_time = ?
var utc_end_time = ?

问题是如何将特定时区的时间转换为 UTC 时间。或者还有其他像样的解决方案吗?谢谢!

编辑:

我想出了一个自己制作的方法,这不太体面。

var converted = moment.tz(moment(), timezone).format("YYYY-MM-DD");         
var full_format = "YYYY-MM-DD HH:mm:ss";
var start = converted + " 00:00:00";
var end = converted + " 23:59:59";
var utc_start_time = moment(start).add(utc_offset * -1, 'hours').format(full_format);
var utc_end_time = moment(end).add(utc_offset * -1, 'hours').format(full_format); 

欢迎任何改进建议。谢谢

取决于你想要做什么:

// for the current day
var start = moment.tz(timezone).startOf('day').utc();
var end = moment.tz(timezone).endOf('day').utc();
// for a specific moment (m)
var start = m.clone().tz(timezone).startOf('day').utc();
var end = m.clone().tz(timezone).endOf('day').utc();
// for a specific calendar date
var m = moment.tz(date, format, timezone);
var start = m.clone().startOf('day').utc();
var end = m.clone().endOf('day').utc();

然后,您可以使用format功能格式化startend

另请记住,并非每个时区的每天从午夜开始,也不是所有当地日都有 24 小时。

我也遇到了这个问题。 不幸的是,时刻.js似乎在告诉人们使用其他选择。(我猜他们更关注美国政治,从他们的网站来看,呃)。 此外,接受的答案中的许多 momemt 功能现在都被剥夺了。 Luxon是第一个建议作为替代方案的图书馆。

let DateTime = luxon.DateTime;
var dt = DateTime.local() // get the current time in local timezone
  .startOf('day')         // set the time to the start of the current day
  .setZone('GMT');        // change time zone back to GMT (zero offset)
console.log('Start: ' + dt.toISO());  // UTC equivalent for local start of day
var dt = DateTime.local() // get the current time in local timezone
  .endOf('day')           // set the time to the end of the current day
  .setZone('GMT');        // change time zone back to GMT (zero offset)
console.log('End  : ' + dt.toISO());  // UTC equivalent for local end of day (1 ms before midnight)
<script src="https://cdn.jsdelivr.net/npm/luxon@1.22.2/build/global/luxon.min.js"></script>

下面是一个本机解决方案,它使用 Intl.DateTimeFormat 来处理时区。

它可以找到指定时区的一天的开始/结束。

要将Date实例转换为 UTC 时间,请使用 Date.prototype.toISOString。

const beginingOfDay = (options = {}) => {
  const { date = new Date(), timeZone } = options;
  const parts = Intl.DateTimeFormat("en-US", {
    timeZone,
    hourCycle: "h23",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
  }).formatToParts(date);
  const hour = parseInt(parts.find((i) => i.type === "hour").value);
  const minute = parseInt(parts.find((i) => i.type === "minute").value);
  const second = parseInt(parts.find((i) => i.type === "second").value);
  return new Date(
    1000 *
      Math.floor(
        (date - hour * 3600000 - minute * 60000 - second * 1000) / 1000
      )
  );
};
const endOfDay = (...args) =>
  new Date(beginingOfDay(...args).getTime() + 86399999);
const beginingOfYear = () => {};
console.log(beginingOfDay({ timeZone: "GMT" }));
console.log(endOfDay({ timeZone: "GMT" }));
console.log(beginingOfDay({ timeZone: "Asia/Tokyo" }));
console.log(endOfDay({ timeZone: "Asia/Tokyo" }));