Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from UTC to Central Time ('America/Chicago') using moment.js

Tags:

momentjs

I believe this instruction should convert correctly from UTC to CST but seems like is an hour off

moment.utc('07-18-2013 16:10:11', 'MM-DD-YYYY HH:mm').tz('America/Chicago').format("YYYY-MM-DD HH:mm");

It returs 2013-07-18 11:10 when it should return 2013-07-18 10:10

Am I missing something?

Edit: If I print the abbreviated Time Zone I get CDT instead of CST. How can I specify CST?

like image 972
ilovelamp Avatar asked Feb 05 '23 07:02

ilovelamp


1 Answers

Your code is correct, and as VincenzoC pointed out in comments, DST is in effect at that time, which is why you get the result you did.

You're using America/Chicago, which means literally - the time in Chicago. It is representative of the entire Central time zone in the United States. There are no places in the US that use Central time without daylight saving time, so asking for that would be nonsensical/invalid.

If you really didn't mean US Central time, your other options would be:

  • Choose the correct time zone.

    .tz('America/Regina')        // Saskatchewan, Canada     (UTC-6 all year)
    .tz('America/Tegucigalpa')   // Honduras, South America  (UTC-6 all year)
    
  • Use a fixed offset instead of a time zone. (You won't need the moment-timezone add-on for this.)

    .utcOffset('-06:00')
    
like image 82
Matt Johnson-Pint Avatar answered Apr 27 '23 03:04

Matt Johnson-Pint