Skip to content

Instantly share code, notes, and snippets.

@ktraff
Created August 14, 2014 18:58
Show Gist options
  • Save ktraff/394075c2b415a8c92a4f to your computer and use it in GitHub Desktop.
Save ktraff/394075c2b415a8c92a4f to your computer and use it in GitHub Desktop.
Convert UTC dates to local time using Moment.js
$(function(){
setInterval(function(){
var divUtc = $('#divUTC');
var divLocal = $('#divLocal');
//put UTC time into divUTC
divUtc.text(moment.utc().format('YYYY-MM-DD HH:mm:ss'));
//get text from divUTC and conver to local timezone
var localTime = moment.utc(divUtc.text()).toDate();
localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');
divLocal.text(localTime);
},1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment