如何在 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
功能格式化start
和end
。
另请记住,并非每个时区的每天从午夜开始,也不是所有当地日都有 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" }));
相关文章:
- 从工作日结束到下一个工作日开始的完整日历JS包装时间
- 如何在dhtmlxgantt中显示灯箱中的开始日期、结束日期和持续时间
- 如何使用jquery获取结束时间
- 基础日期选择器、结束日期(和时间)不能早于开始日期(和时间)
- 结束日期作为基础日期时间选择器中的选定开始日期
- 在for循环中另一个音频结束后播放一段时间的音频
- 如何在Dynamics CRM中获取时区的夏令时开始和结束时间
- 识别有角度的spa应用程序页面渲染结束时间
- 尝试创建Javascript表单来计算结束时间
- Javascript REGEX:需要从嵌入URL中检索ID以及开始和结束时间
- 如何在 javascript 中获取指定时区的一天的开始时间和结束时间(以 UTC 为单位)
- 如何在时间结束时发出警报
- 如何在 jQuery 中时间结束时发出警报
- j查询开始时间/结束时间验证
- 计时器在时间结束时发出警报
- 如何通过正确切换在最长执行时间结束时给出用户定义的超时消息
- 当Css3动画被相同的事件触发时,具有相同持续时间的动画在不同的时间结束
- 创建'时间结束'从价值
- 禁用Onclick直到持续时间结束
- 使用javascript验证日期、时间开始和时间结束