javascript - Force Moment.js format() to ignore daylight savings time JS - Stack Overflow

I am currently in PST at -07:00 from UTC.If I do this: moment().format('Z') I correctly get -

I am currently in PST at -07:00 from UTC.

If I do this: moment().format('Z') I correctly get -07:00. Is there a way to (simply) force moment.format() to ignore daylight savings time, (and in my case give me -08:00)?

I am currently in PST at -07:00 from UTC.

If I do this: moment().format('Z') I correctly get -07:00. Is there a way to (simply) force moment.format() to ignore daylight savings time, (and in my case give me -08:00)?

Share Improve this question asked Jul 5, 2018 at 23:05 AlexKAlexK 4268 gold badges24 silver badges45 bronze badges 3
  • It's a bit unclear what you're asking. I think you want to use utcOffset (moment().utcOffset("-08:00").format("Z") === "-08:00"), but it sounds like you might want it generic to include all timezones. – Heretic Monkey Commented Jul 5, 2018 at 23:15
  • Correct. I would like it to be generic to include all zones. – AlexK Commented Jul 5, 2018 at 23:16
  • Sounds like you want something more like Moment Timezone: momentjs./timezone/docs/#/zone-object – Heretic Monkey Commented Jul 5, 2018 at 23:19
Add a ment  | 

3 Answers 3

Reset to default 5

The following code will do what you asked:

// First we need to get the standard offset for the current time zone.
// Since summer and winter are at opposite times in northern and summer hemispheres,
// we will take a Jan 1st and Jul 1st offset.  The standard offset is the smaller value
// (If DST is applicable, these will differ and the daylight offset is the larger value.)
var janOffset = moment({M:0, d:1}).utcOffset();
var julOffset = moment({M:6, d:1}).utcOffset();
var stdOffset = Math.min(janOffset, julOffset);

// Then we can make a Moment object with the current time at that fixed offset
var nowWithoutDST = moment().utcOffset(stdOffset);

// Finally, we can format the object however we like. 'Z' provides the offset as a string.
var offsetAsString = nowWithoutDST.format('Z');

However: You should probably ask yourself why you want to do this. In the vast majority of cases, ignoring DST when it is actually in effect is an error. Your users don't have a choice of whether or not they use DST. If it's applicable in their local time zone, then your code needs to also take it into account. Don't try to defeat it.

On the other hand, if you're simply displaying what the time or offset would be without DST for informational purposes, that is probably acceptable.

I think you will need to get the offset from a non-DST timestamp and then munge the strings.

Assuming a date of some kind, this should get you a string which is date + DST-ignorant-offset:

function _noDST(noDST, dateStr){
    var beginningOfTimeNoDST = moment(noDST); // whatever target you know has no DST
    var d = moment(dateStr);
    var noOffset = d.format('YYYY-MM-DDTHH:mm:ss');
    var offset = beginningOfTimeNoDST.format('Z');
    return [noOffset, offset].join("");
}

This should subtract an hour during DST, if that’s what you want.

moment().subtract(moment().isDST() ? 1 : 0, ‘hours’).format('Z')

Update: As noted in ments this only works in cases where you know the timezone is 1 hour ahead during DST.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744209576a4563271.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信