0

I am using moment.js and moment-timezone. how can I create a function to convert 2016-06-12 06:22:18 UTC time to the user's timezone time and then format the time to Jun 12, 2015 6:22 PM


for example:

updateTime("2016-06-12 06:22:18");

if in chicago, the output will be:

"Jun 12, 2016, 1:22 AM"
zanderwar
  • 3,440
  • 3
  • 28
  • 46
johnjay22113
  • 133
  • 3
  • 10

4 Answers4

1

You can try something like this.

moment.utc("2016-06-12 06:22:18", "YYYY-MM-DD HH:mm:ss").tz('America/Chicago').format("MMM D, YYYY, hh:mm a")
s7vr
  • 73,656
  • 11
  • 106
  • 127
0

The function you need would be like this :

function getLocalDate (input)
{
  var dt= moment.utc(input);
  dt.add(moment().utcOffset(),'minutes');
  return moment(dt).format('MMM D, YYYY, hh:mm a');
};

Or this :

function getLocalDate(input)
{
  return moment.utc(input).local().format('MMM D, YYYY, hh:mm a');
};
akardon
  • 43,164
  • 4
  • 34
  • 42
0

Hey this links may help you

https://gist.github.com/founddrama/2182389

https://www.sitepoint.com/managing-dates-times-using-moment-js/

http://momentjs.com/timezone/docs/

Examples:

moment().format('MMMM Do YYYY, h:mm:ss a'); // June 20th 2016, 11:10:20 am
moment().format('dddd');                    // Monday
moment().format("MMM Do YY");               // Jun 20th 16
moment().format('YYYY [escaped] YYYY');     // 2016 escaped 2016
moment().format();                       
moment.tz('2016-01-01', 'America/Chicago').format('z');  
Shankar Shastri
  • 1,134
  • 11
  • 18
0

You might want to look at this answer, perhaps: Get current timestamp from specific timezone

They solved by taking the time in UTC and converting it in the specific time

var utcEpochSeconds = dateObj.getTime() + (dateObj.getTimezoneOffset() * 60000);

Community
  • 1
  • 1
Adriano
  • 3,788
  • 5
  • 32
  • 53