Get timezone difference in hours with moment(以小时为单位获取时区差异)
问题描述
我想通过 Node.js moment 模块获取纽约和香港之间的时区差异.我已经做了一些前期工作.
I would like to get the timezone difference between New York and Hong Kong with Node.js moment module. I have done some preliminary work.
var NewYork_time_hr = moment().tz("America/New_York").format('HH');
var HongKong_time_hr = moment().tz("Asia/Hong_Kong").format('HH');
然后我可以继续编写一个函数来计算两个时区之间的时间差(以小时为单位).我希望有一个更简单的方法.
I can then proceed to write a function to calculate the difference between the 2 timezones in hours. I was hoping for a simpler method.
有没有更优雅、更简单的方式来使用矩库?
Is there a more elegant and simpler way to do it with moment library?
推荐答案
不确定更简单",但更正确(因为并非所有时区都相隔一小时):
Not sure about "simpler", but more correct (since not all timezones are a full hour from each other):
// get the current time so we know which offset to take (DST is such bullkitten)
var now = moment.utc();
// get the zone offsets for this time, in minutes
var NewYork_tz_offset = moment.tz.zone("America/New_York").offset(now);
var HongKong_tz_offset = moment.tz.zone("Asia/Hong_Kong").offset(now);
// calculate the difference in hours
console.log((NewYork_tz_offset - HongKong_tz_offset) / 60);
<script src="aHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvbW9tZW50LmpzLzIuMTAuNi9tb21lbnQubWluLmpz"></script>
<script src="aHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvbW9tZW50LXRpbWV6b25lLzAuNC4xL21vbWVudC10aW1lem9uZS13aXRoLWRhdGEtMjAxMC0yMDIwLm1pbi5qcw=="></script>
这篇关于以小时为单位获取时区差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以小时为单位获取时区差异


- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06