0

I'm trying to convert the start date of an event in UTC time to America/Chicago time using moment.js

The given input of the start date is '2021-02-10T03:05:00Z'.

I am trying to convert that input to a start time of '2012-02-09' at 9:00PM CST.

Any input is appreciated!

Danny Sun
  • 31
  • 7
  • 1
    Does this answer your question? [Generating a date in a particular timezone](https://stackoverflow.com/questions/19370849/generating-a-date-in-a-particular-timezone) – Nik Feb 09 '21 at 07:34

2 Answers2

1

All you need to do is set the timeZone like this

var moment = require('moment-timezone');
moment('2021-02-10T03:05:00Z').tz("America/Los_Angeles").format('LL');

The timeZone isn't the one you are looking for, to get a list of the timeZone names you need to call moment.tz.zonesForCountry('US');

It should get you a list of all the timezones for your country then you just have to pick the right one for Chicago, and then you can format it like you want it displayed.

Double Bang
  • 184
  • 15
  • Thanks for the response! so i did this `moment('2021-02-10T03:05:00Z').tz("America/Chicago").format()` and it gave me `2021-02-10T03:05:00-06:00` was hoping it would give me this date 2012-02-09 with 9pm – Danny Sun Feb 09 '21 at 07:59
  • Did you import `moment-timezone` instead of `moment` ? – Double Bang Feb 09 '21 at 15:37
  • Yep, required moment-timezone instead of moment and did `moment('2021-02-10T03:05:00Z').tz("America/Chicago").format('LL')` and it yields `February 10, 2021` – Danny Sun Feb 09 '21 at 20:40
  • Maybe try it like this => `moment.tz('2021-02-10T03:05:00Z', "America/Chicago").format('LLLL')` both ways work on my side, I'll correct my answer if that way works for you. – Double Bang Feb 10 '21 at 07:36
-1

You can use moment-timezone for this. Here is the working code example.

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>

<div id="utc-time">09/02/2021 12:40:35 PM</div>
<div id="cst-time"></div>
<div id="aus-time"></div>

var dt = document.getElementById("utc-time").innerHTML;  
var m = moment.utc(dt, "DD-MM-YYYY h:mm:ss A"); // parse input as UTC
console.log(m.clone().local().format("DD-MM-YYYY h:mm:ss A")); // local time
document.getElementById("utc-time").innerHTML = m.clone().local().format("DD-MM-YYYY h:mm:ss A (Z)");
var tz = 'America/New_York'; // timezone value, you can use moment.tz.guess() for automatic detection
document.getElementById("cst-time").innerHTML = m.clone().tz(tz).format("DD-MM-YYYY h:mm:ss A (Z)");
tz = 'Australia/Perth'; // test another timezone
document.getElementById("aus-time").innerHTML = m.clone().tz(tz).format("DD-MM-YYYY h:mm:ss A (Z)");

Working fiddle: https://jsfiddle.net/9c2f068z/

CodeRunner
  • 176
  • 3
  • 7