javascript 如何在不创建新的时刻对象的情况下获得所需时区的小时和分钟?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34324208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:33:33  来源:igfitidea点击:

How to get hours and minutes in desired timezone without creating new moment object?

javascripttimezonemomentjs

提问by user1261710

I have to display a string on the web page in this format: 16:00 HH:mm

我必须以这种格式在网页上显示一个字符串: 16:00 HH:mm

I'm using a moment object to represent a date/time and timezone.

我正在使用一个时刻对象来表示日期/时间和时区。

var day = moment().tz('GMT');
day.hours(16).minutes(0).seconds(0).milliseconds(0);

So this is 16:00 in GMT time.

所以这是格林威治标准时间的 16:00。

On my web page I want to change the time zone and then collect the hours and minutes.

在我的网页上,我想更改时区,然后收集小时和分钟。

If I make a new moment object

如果我创建一个新的时刻对象

var day2 = moment().tz('PST); //this is 8 AM since gmt was 16
console.log(day2.get('hours'));

it is 16 not 8!

是 16 不是 8!

and try to get the hours and minutes they are in GMT not in PST.

并尝试获取它们在 GMT 而非 PST 中的小时数和分钟数。

How can I get it in PST? Do I have to keep wrapping it?

我怎样才能在 PST 中获得它?我必须继续包装它吗?

回答by Matt Johnson-Pint

// 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'));

Working jsFiddle here.

在这里工作 jsFiddle。

If you can't get it to work, then you haven't correctly loaded moment, moment-timezone, and the required time zone data. For the data, you either need to call moment.tz.addwith the zone data for the zones you care about, or you need to use one of the moment-timezone-with-data files available on the site.

如果你不能让它工作,那么你没有正确加载时刻、时刻-时区和所需的时区数据。对于数据,您需要moment.tz.add使用您关心的区域的区域数据调用,或者您需要使用站点上可用的 moment-timezone-with-data 文件之一。

In the fiddle, you can see the moment-files I'm loading by expanding the External Resources section.

在小提琴中,您可以通过展开外部资源部分来查看我正在加载的时刻文件。

fiddle resources

小提琴资源

回答by STLMikey

PST can mean different things in different regions. In the moment-timezone docs, I see nothing referring to "PST" or similar abbreviations.

PST 在不同地区可能意味着不同的东西。在 moment-timezone 文档中,我没有看到任何提到“PST”或类似缩写的内容。

Perhaps try:

也许尝试:

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

回答by RobG

I don't know about using moment.js, but it's fairly simple using POJS and the same algorithm should work. Just subtract 8 hours from the UTC time of a date object and return a formatted string based on the adjusted UTC time.

我不知道使用 moment.js,但使用 POJS 相当简单,并且相同的算法应该可以工作。只需从日期对象的 UTC 时间减去 8 小时,并根据调整后的 UTC 时间返回一个格式化字符串。

Assuming PST is "Pacific Standard Time", also known as "Pacific Time" (PT), and is UTC -8:00:

假设 PST 是“太平洋标准时间”,也称为“太平洋时间”(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));

There is moment-timezonewhich adds functionality to moment.js for IANA time zones. For PST you can use America/Los_Angeles, however it might also automatically adjust for daylight saving so you'll get PDT when that applies. If you want ignore daylight saving, use the above or find a location with the offset you need and use that.

moment-timezoneIANA 时区的moment.js 添加了功能。对于 PST,您可以使用 America/Los_Angeles,但它也可能会根据夏令时自动调整,以便您在适用时获得 PDT。如果您想忽略夏令时,请使用上述方法或找到具有您需要的偏移量的位置并使用它。