user45867
user45867

Reputation: 983

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

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.

Upvotes: 10

Views: 45836

Answers (6)

Nilesh Patel
Nilesh Patel

Reputation: 137

You can use below code snippet

// Get time zone offset for CDT or CST
const getCdtCstOffset = () => {
  const getNthSunday = (date, nth) => {
      date.setDate((7*(nth-1))+(8-date.getDay()));
      return date;
  }
  const isCdtTimezoneOffset = (today) => {
    console.log('Today : ', today);
    let dt = new Date();
    var mar = new Date(dt.getFullYear(), 2, 1);
    mar = getNthSunday(mar, 2);
    console.log('CDT Start : ', mar);
    var nov = new Date(dt.getFullYear(), 10, 1, 23, 59, 59);
    nov = getNthSunday(nov, 1);
    console.log('CDT End : ', nov);
    return mar.getTime()< today.getTime() && nov.getTime()> today.getTime();
  }
  var today = new Date()// current date
  if (isCdtTimezoneOffset(today)) {
    return -5
  } else {
    return -6
  }
}
let cstOrCdt = new Date();
cstOrCdt.setHours(cstOrCdt.getHours()+getCdtCstOffset())
console.log('CstOrCdt : ', cstOrCdt);

Upvotes: -1

Brenda Jimenez
Brenda Jimenez

Reputation: 761

Maybe you can use something like the following. Note, that is just an example you might need to adjust it to your needs.

    let cstTime = new Date(createdAt).toLocaleString("es-MX", {
    timeZone: "America/Mexico_City" });

Upvotes: 1

Subhasis Chakraborty
Subhasis Chakraborty

Reputation: 41

let newDate = moment(new Date()).utc().format("YYYY-MM-DD HH:mm:ss").toString()
var m = moment.utc(newDate);
m.tz('America/Chicago');
var cstDate = m.format("YYYY-MM-DD HH:mm:ss");

Upvotes: 0

Suneet Patil
Suneet Patil

Reputation: 618

You can use below code snippet for converting.

function convertUTCtoCDT() {
    var timelagging = 6; // 5 or 6
    var utc = new Date();
    var cdt = new Date(utc.getTime()-((1 * 60 * 60 * 1000) * timelagging));
    console.log("CDT: "+cdt);
}

Upvotes: 0

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241693

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).

Upvotes: 8

WhiteHat
WhiteHat

Reputation: 61230

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>

Upvotes: 5

Related Questions