最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to disable moment.js daylight timezone conversion - Stack Overflow

programmeradmin1浏览0评论

It is possible to disable the daylight timezone conversion in moment.js?

      $scope.obj.date = moment('2016-06-03T04:00:00.000Z');

Basically my application deals with events and dates only, but moment.js converting the daylight savings time is causing issue with dates. Does it have any setting which will disable it across the entire application usage?

It is possible to disable the daylight timezone conversion in moment.js?

http://plnkr.co/edit/MjFelt?p=preview

      $scope.obj.date = moment('2016-06-03T04:00:00.000Z');

Basically my application deals with events and dates only, but moment.js converting the daylight savings time is causing issue with dates. Does it have any setting which will disable it across the entire application usage?

Share Improve this question asked Jun 13, 2016 at 19:40 josh_boazjosh_boaz 2,0237 gold badges35 silver badges74 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 14

If you are saying that you want moment to display your date and time (which is UTC, as indicated by the 'Z'), exactly as is, you should use moment.utc:

moment.utc('2016-06-03T04:00:00.000Z').format()
"2016-06-03T04:00:00Z"

When you use the default moment constructor, as you are now, you are telling moment to convert your UTC time to local time, and this is why you are seeing a time difference. For instance, on my local machine (I am currently UTC-5) I get the following:

moment('2016-06-03T04:00:00.000Z').format()
"2016-06-02T23:00:00-05:00"

This question comes up quite a lot, so I wrote this blog post that explains moment's constructor functions and how it converts ISO8601 dates in detail: https://maggiepint.com/2016/05/14/moment-js-shows-the-wrong-date/

In my case, I had a problem with timezone changing due to 'daylight saving time'.

I had the next period:

{
    "from": "2020-10-01 00:00:00 +0200",
    "to":"2020-11-01 00:00:00 +0100",
}

And I wanted to get:

{
    "from": "2020-10-01 00:00:00 +0200",
    "to":"2020-11-01 00:00:00 +0200",
}

My solution is to get current (local) timezone and set it to the both date moment:

const currentTzOffset = moment().utcOffset(); // getting current timezone offset

const startToLocalZone = moment(from, yourDateFormat)
    .local() // just checking. not sure if this is necessary
    .utcOffset(currentTzOffset) // put your tz to here
    .format(yourDateFormat);
const endToLocalZone = moment(to, yourDateFormat)
    .local() // just checking. not sure if this is necessary
    .utcOffset(currentTzOffset) // put your tz to here
    .format(yourDateFormat);

console.log(startToLocalZone); // output: "2020-10-01 00:00:00 +0200"
console.log(endToLocalZone); // output: "2020-11-01 00:00:00 +0200"

And dont forget to set your date format instead 'yourDateFormat'

var tz = 'America/Vancouver';           // or whatever your time zone is
var dt = '2022-03-13T07:00:00.101Z';    // or whatever date/time you're working with
var momentVal =  moment.tz(dt,tz)

function isNextDayDST(mObj){
  return mObj.clone().add(1, 'days').isDST();
}

function isTodayDST(mObj) {
  return mObj.clone().isDST();
}

function getDSTHourCompensation(mObj) {
 const todayDST = isTodayDST(mObj.clone());
 const tomorrowDST = isNextDayDST(mObj.clone());

 if(todayDST == false && tomorrowDST == true) {
    return 1
 }
 if(todayDST == true && tomorrowDST == false) {
   return -1
 }
 return 0
}

function removeDST(mObj){
  const hourCompentation = getDSTHourCompensation(mObj);
  return mObj.clone().add(hourCompentation, 'hours');
}


console.log(momentVal.format('YYYY-MM-DD HH:mm'))
console.log(removeDST(momentVal).format('YYYY-MM-DD HH:mm'))

Maybe use the moment lib to compensate for the hours when you want to remove DST for the particular case.

发布评论

评论列表(0)

  1. 暂无评论