如何在不创建新的时刻对象的情况下获得所需时区的小时和分钟?
How to get hours and minutes in desired timezone without creating new moment object?
我必须以这种格式在网页上显示一个字符串:16:00 HH:mm
我正在使用 moment 对象来表示 date/time 和时区。
var day = moment().tz('GMT');
day.hours(16).minutes(0).seconds(0).milliseconds(0);
格林威治标准时间16:00。
我想在我的网页上更改时区,然后收集小时和分钟。
如果我创建一个新的 moment 对象
var day2 = moment().tz('PST); //this is 8 AM since gmt was 16
console.log(day2.get('hours'));
是 16 不是 8!
并尝试获取他们在格林威治标准时间而不是太平洋标准时间的小时和分钟。
如何在 PST 中获取它?我必须一直包裹它吗?
PST 在不同地区可能有不同的含义。在 moment-timezone 文档中,我没有看到任何提及 "PST" 或类似缩写的内容。
也许试试:
var day2 = moment().tz('PST');
// 16 with Error: Moment Timezone has no data for PST. See http://momentjs.com/timezone/docs/#/data-loading/.
var day2 = moment().tz('America/Los_Angeles');
// 15
我不知道如何使用 moment.js,但使用 POJS 相当简单,同样的算法应该可以工作。只需从日期对象的 UTC 时间中减去 8 小时,然后 return 一个基于调整后的 UTC 时间的格式化字符串。
假设 PST 是 "Pacific Standard Time",也称为 "Pacific Time" (PT),并且是 UTC -8:00:
/* @param {Date} date - input date object
** @returns {string} - time as hh:mm:ss
**
** Subtract 8 hours from date UTC time and return a formatted times string
*/
function getPSTTime(date) {
var d = new Date(+date);
d.setUTCHours(d.getUTCHours() - 8);
return ('0' + d.getUTCHours()).slice(-2) + ':' +
('0' + d.getUTCMinutes()).slice(-2) + ':' +
('0' + d.getUTCSeconds()).slice(-2);
}
document.write('Current PST time: ' + getPSTTime(new Date));
有moment-timezone which adds functionality to moment.js for IANA time zones。对于 PST,您可以使用 America/Los_Angeles,但它也可能会自动调整夏令时,因此您将在适用时获得 PDT。如果您想忽略夏令时,请使用上面的方法或找到具有您需要的偏移量的位置并使用它。
// initialize a new moment object to midnight UTC of the current UTC day
var m1 = moment.utc().startOf('day');
// set the time you desire, in UTC
m1.hours(16).minutes(0);
// clone the existing moment object to create a new one
var m2 = moment(m1); // OR var m2 = m1.clone(); (both do the same thing)
// set the time zone of the new object
m2.tz('America/Los_Angeles');
// format the output for display
console.log(m2.format('HH:mm'));
如果您无法让它工作,那么您没有正确加载时刻、时刻-时区和所需的时区数据。对于数据,您需要使用您关心的区域的区域数据调用 moment.tz.add
,或者您需要使用网站上可用的 moment-timezone-with-data 文件之一。
在 fiddle 中,您可以通过展开“外部资源”部分来查看我正在加载的时刻文件。
我必须以这种格式在网页上显示一个字符串:16:00 HH:mm
我正在使用 moment 对象来表示 date/time 和时区。
var day = moment().tz('GMT');
day.hours(16).minutes(0).seconds(0).milliseconds(0);
格林威治标准时间16:00。
我想在我的网页上更改时区,然后收集小时和分钟。
如果我创建一个新的 moment 对象
var day2 = moment().tz('PST); //this is 8 AM since gmt was 16
console.log(day2.get('hours'));
是 16 不是 8!
并尝试获取他们在格林威治标准时间而不是太平洋标准时间的小时和分钟。
如何在 PST 中获取它?我必须一直包裹它吗?
PST 在不同地区可能有不同的含义。在 moment-timezone 文档中,我没有看到任何提及 "PST" 或类似缩写的内容。
也许试试:
var day2 = moment().tz('PST');
// 16 with Error: Moment Timezone has no data for PST. See http://momentjs.com/timezone/docs/#/data-loading/.
var day2 = moment().tz('America/Los_Angeles');
// 15
我不知道如何使用 moment.js,但使用 POJS 相当简单,同样的算法应该可以工作。只需从日期对象的 UTC 时间中减去 8 小时,然后 return 一个基于调整后的 UTC 时间的格式化字符串。
假设 PST 是 "Pacific Standard Time",也称为 "Pacific Time" (PT),并且是 UTC -8:00:
/* @param {Date} date - input date object
** @returns {string} - time as hh:mm:ss
**
** Subtract 8 hours from date UTC time and return a formatted times string
*/
function getPSTTime(date) {
var d = new Date(+date);
d.setUTCHours(d.getUTCHours() - 8);
return ('0' + d.getUTCHours()).slice(-2) + ':' +
('0' + d.getUTCMinutes()).slice(-2) + ':' +
('0' + d.getUTCSeconds()).slice(-2);
}
document.write('Current PST time: ' + getPSTTime(new Date));
有moment-timezone which adds functionality to moment.js for IANA time zones。对于 PST,您可以使用 America/Los_Angeles,但它也可能会自动调整夏令时,因此您将在适用时获得 PDT。如果您想忽略夏令时,请使用上面的方法或找到具有您需要的偏移量的位置并使用它。
// initialize a new moment object to midnight UTC of the current UTC day
var m1 = moment.utc().startOf('day');
// set the time you desire, in UTC
m1.hours(16).minutes(0);
// clone the existing moment object to create a new one
var m2 = moment(m1); // OR var m2 = m1.clone(); (both do the same thing)
// set the time zone of the new object
m2.tz('America/Los_Angeles');
// format the output for display
console.log(m2.format('HH:mm'));
如果您无法让它工作,那么您没有正确加载时刻、时刻-时区和所需的时区数据。对于数据,您需要使用您关心的区域的区域数据调用 moment.tz.add
,或者您需要使用网站上可用的 moment-timezone-with-data 文件之一。
在 fiddle 中,您可以通过展开“外部资源”部分来查看我正在加载的时刻文件。