如何获取javascript中指定时区一天的开始时间和结束时间(UTC格式)?

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

如标题所述,我想获取特定时区一天的开始时间和结束时间,并将它们转换为 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 小时。

这是一个使用 Intl.DateTimeFormat 处理时区的本机解决方案。

它可以找到指定时区的日期begining/end。

要将 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" }));

我运行也遇到了这个问题。不幸的是,moment.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>