Moment js default timezone not working when creating date

12,752

1. Solution: Moment timezone setDefault is affecting the instances created after setDefault. If you debug like this:

console.log("Currrent timezone: "); console.log(moment().tz());
console.log("Current time: "); console.log(moment().format());
console.log("Updating timezone: "); console.log("America/Lima");
moment.tz.setDefault("America/Lima");
console.log("Current time: "); console.log(moment().format());
console.log("Currrent timezone after updating: "); console.log(moment().tz());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You can see the setDefault is working.

2. Solution: .local() function sets a flag on the original moment to use local time to display a moment instead of the original moment's time. Even if you use moment.tz.setDefault to alter the timezone, it gets the computer's timezone.

console.log(moment(new Date()).format());
moment.tz.setDefault("America/Lima");
console.log(moment(new Date()).local().format());
console.log(moment(new Date()).format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Share:
12,752

Related videos on Youtube

Syed Aqeel Ashiq
Author by

Syed Aqeel Ashiq

Updated on September 16, 2022

Comments

  • Syed Aqeel Ashiq
    Syed Aqeel Ashiq over 1 year

    Please refer to the following code snippets.

    Problem 1: I am setting default timezone for moment js. But right after setting it, If I retrieve the timezone from moment it is not what I just set.

    console.log("Currrent timezone: "); console.log(moment.tz());
    console.log("Updating timezone: "); console.log("America/Lima");
    moment.tz.setDefault("America/Lima");
    console.log("Currrent timezone after updating: "); console.log(moment.tz());
    

    Following log is printed:

    Currrent timezone: Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Nov 15 2017 13:39:45 GMT+0500 (Pakistan Standard Time), …}
    Updating timezone: America/Lima
    Currrent timezone after updating: Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Nov 15 2017 13:39:45 GMT+0500 (Pakistan Standard Time), …}
    

    Problem 2: Also another problem is that, the default timezone which I set is applied to moment.format() function but not applied when creating new date objects from moment.

    console.log(moment(new Date()).toDate()); // this uses user browser timezone
    // prints Wed Nov 15 2017 13:57:40 GMT+0500 (Pakistan Standard Time)
    // I want it to give the time in the timezone which i just set above
    
    console.log(moment(new Date()).local().toDate());
    // prints Wed Nov 15 2017 13:57:40 GMT+0500 (Pakistan Standard Time)
    
    console.log(moment(new Date()).format()); // this uses the timezone which I set above in moment default timezone. This is correct intended behavior
    // prints: 2017-11-15T03:57:40-05:00
    
  • Optimae
    Optimae over 5 years
    In case it helps anyone: Make sure you're using the moment-timezone-with-data.min.js file, which you might miss if you're in a rush! (The moment-timezone.js one won't work for setting defaults because it lacks the data)