How do I convert UTC/ GMT datetime to CST in Javascript? (not local, CST always)

29,390

Solution 1

Using moment.js with the moment-timezone add-on makes this task simple.

// construct a moment object with UTC-based input
var m = moment.utc('2015-01-01 00:00:00');

// convert using the TZDB identifier for US Central time
m.tz('America/Chicago');

// format output however you desire
var s = m.format("YYYY-MM-DD HH:mm:ss");

Additionally, since you are referring to the entire North American Central time zone, you should say either "Central Time", or "CT". The abbreviation "CST" as applied to North America explicitly means UTC-6, while the "CDT" abbreviation would be used for UTC-5 during daylight saving time.

Do be careful with abbreviations though. "CST" might mean "China Standard Time". (It actually has five different interpretations).

Solution 2

You can use the time zone offset to determine whether 5 or 6 hours should be subtracted.

var dateJan;
var dateJul;
var timezoneOffset;

var divUTC;
var divCST;

// Set initial date value
dateValue = new Date('10/31/2015 7:29:54 PM');

divUTC = document.getElementById('UTC_Time');
divCST = document.getElementById('CST_Time');
divUTC.innerHTML = 'from UTC = ' + dateValue.toString();

// Get dates for January and July
dateJan = new Date(dateValue.getFullYear(), 0, 1);
dateJul = new Date(dateValue.getFullYear(), 6, 1);

// Get timezone offset
timezoneOffset = Math.max(dateJan.getTimezoneOffset(), dateJul.getTimezoneOffset());

// Check if daylight savings
if (dateValue.getTimezoneOffset() < timezoneOffset) {
  // Adjust date by 5 hours
  dateValue = new Date(dateValue.getTime() - ((1 * 60 * 60 * 1000) * 5));
}
else {
  // Adjust date by 6 hours
  dateValue = new Date(dateValue.getTime() - ((1 * 60 * 60 * 1000) * 6));
}

divCST.innerHTML = 'to CST = ' + dateValue.toString();
<div id="UTC_Time"></div>
<br/>
<div id="CST_Time"></div>
Share:
29,390
user45867
Author by

user45867

Updated on July 25, 2022

Comments

  • user45867
    user45867 almost 2 years

    I have a challenge where backend data is always stored in UTC time. Our front-end data is always presented in CST. I don't have access to this 'black box.'

    I would like to mirror this in our data warehouse. Which is based in Europe (CET). So "local" conversion will not work.

    I'm wondering the simplest, most straightforward way to accurately convert UTC time (I can have it in epoch milliseconds or a date format '2015-01-01 00:00:00') to Central Standard Time. (which is 5 or 6 hours behind based on Daylight Savings).

    I see a lot of threads about converting to 'local' time ... again I don't want this, nor do I simply want to subtract 6 hours which will be wrong half the year.

    Anyone have any ideas? This seems to be a very common problem but I've been searching for a while, and have found nothing.

  • user45867
    user45867 over 8 years
    hmm I will have to try this
  • Matt Johnson-Pint
    Matt Johnson-Pint over 8 years
    Nice try, but flawed. The Date object will only tell you if the local time zone is in DST or not. DST is not uniformly applied worldwide.
  • Matt Johnson-Pint
    Matt Johnson-Pint over 8 years
    You're also interpreting the input value in the local time zone, not in UTC, and are using the locale-specific MM/DD/YYYY format in the string. Additionally, shifting the time itself is not the safest way to adjust for time zone. These errors are all too easy to make, especially in JavaScript. Take a good look at moment.js - it will make your life easier. Cheers! :)
  • wa7d
    wa7d over 4 years
    Moment is quite heavy and unnecessary for a simple task like this that you could totally do without moment