Thursday, October 31, 2013

swapping dates as strings from local to UTC time with moment.js

Cast a local time date in a military-time/nothing-but-digits-and-dividers/alphabetical-order-friendly string format to a UTC time military-time/nothing-but-digits-and-dividers/alphabetical-order-friendly string format:

var localString = "1974-8-24 06:13:13";
var pointInTime = moment.utc(localString);
var formattedTime = pointInTime.format('YYYY-MM-D HH:mm:ss');

 
 

Cast a UTC time date in a military-time/nothing-but-digits-and-dividers/alphabetical-order-friendly string format to a local time military-time/nothing-but-digits-and-dividers/alphabetical-order-friendly string format:

var utcString = "1974-8-24 06:13:13 GMT";
var pointInTime = moment(utcString);
pointInTime.local();
var formattedTime = pointInTime.format('YYYY-MM-D HH:mm:ss');

 
 

Using "UTC" in place of "GMT" above seems to work just fine too.

No comments:

Post a Comment