如标题所述,我想获取特定时区的一天的开始时间和结束时间,并将它们转换为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();
然后start
,您可以格式化并根据需要end
使用该format
功能。
还请记住,并非每天都在每个时区的午夜开始,也不是本地的所有时间都没有24小时。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句