如何从时刻对象获得时区偏移?

时间:2016-01-17 12:54:37

标签: javascript node.js momentjs

我将moment对象定义为:

var moment = require('moment');

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

当我打印它时,我得到:

_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

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

谢谢,

1 个答案:

答案 0 :(得分:18)

只需像访问任何对象一样访问该属性

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>

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

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