Javascript 如何从时刻对象获取时区偏移量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34838542/
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-08-23 16:55:50  来源:igfitidea点击:

How to get Timezone offset from moment Object?

javascriptnode.jsmomentjs

提问by snaggs

I have momentObject defined as:

我将moment对象定义为:

var moment = require('moment');

moment('2015-12-20T12:00:00+02:00');

When I print it, I get:

当我打印它时,我得到:

_d: Sun Dec 20 2015 12:00:00 GMT+0200 (EET)
_f: "YYYY-MM-DDTHH:mm:ssZ"
_i: "2015-12-20T12:00:00+02:00"
_isAMomentObject: true
_isUTC: false
_locale: r
_pf: Object
_tzm: 120

How to fetch by right way _tzm? (suppose its offset in minutes)

如何以正确的方式获取_tzm?(假设它的偏移量以分钟为单位)

Thanks,

谢谢,

回答by adeneo

Just access the property like you would in any object

就像在任何对象中一样访问属性

var result = moment('2015-12-20T12:00:00+02:00');


document.body.innerHTML = result._tzm;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>

Another option would be to parse the date and get the zone

另一种选择是解析日期并获取区域

moment.parseZone('2015-12-20T12:00:00+02:00').utcOffset(); // 120
// or
moment().utcOffset('2015-12-20T12:00:00+02:00')._offset; // 120