Skip to content

Instantly share code, notes, and snippets.

@sunitJindal
Last active January 20, 2016 09:47
Show Gist options
  • Save sunitJindal/a4bb2b6a31869a7eec02 to your computer and use it in GitHub Desktop.
Save sunitJindal/a4bb2b6a31869a7eec02 to your computer and use it in GitHub Desktop.
Conversion of date string to date object using Moment.js
// Local time zone is Asia/Kolkata with offset GMT +0530
var dt = "14/03/2016";
var dtz = "14/03/2016 +0130";
var tz = "+0130";
var df = "DD/MM/YYYY";
var dfz = "DD/MM/YYYY Z";
var d1 = moment(dtz,dfz); // date string with time zone
var d2 = moment(dt,df); // date string without timezone
var d3 = moment(dtz,dfz).utcOffset(tz); // date string with time zone. Apply UTC offset after moment is created
var d4 = moment(dt,df).utcOffset(tz); // date string without time zone. Apply UTC offset after moment is created
d1.format()
// "2016-03-14T04:00:00+05:30"
d2.format()
// "2016-03-14T00:00:00+05:30"
d3.format()
// "2016-03-14T00:00:00+01:30"
d4.format()
// "2016-03-13T20:00:00+01:30"
d1.toDate()
// Mon Mar 14 2016 04:00:00 GMT+0530 (IST)
d2.toDate()
// Mon Mar 14 2016 00:00:00 GMT+0530 (IST)
d3.toDate()
// Mon Mar 14 2016 04:00:00 GMT+0530 (IST)
d4.toDate()
// Mon Mar 14 2016 00:00:00 GMT+0530 (IST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment