Created
February 3, 2016 19:58
-
-
Save guptag/22151ac89846eeab5232 to your computer and use it in GitHub Desktop.
MomentJS - DateTime Kung Fu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* Browser display of new Date() or moment().toDate() is always local with info of GMT offset (pacific for e.g) | |
* Anytime format() is called after toDate(), date is converted to local | |
* date object always hold local time, it is .utc.format() or .tz("...").format() converts the date into that timezone | |
* to load UTC time, make sure the date format has ended with "Z" | |
var m = moment(new Date("2015-12-31 12:00:00")) // no Z, so local date | |
m.format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 12:00:00 pm" | |
m.utc().format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 8:00:00 pm" | |
m.tz("US/Eastern").format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 3:00:00 pm" | |
m.tz("US/Eastern").toDate(); --> Thu Dec 31 2015 12:00:00 GMT-0800 (PST) | |
var m2 = moment("2015-12-31T12:00:00Z"); //has Z, so GMT | |
m2.format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 4:00:00 am" // local (pacific) | |
m2.tz("US/Eastern").format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 7:00:00 am" | |
var m3 = moment.tz("2015-12-31 12:00:00Z", "America/New_York"); | |
m3.format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 12:00:00 pm" (eastern) | |
m3.toDate() --> Thu Dec 31 2015 09:00:00 GMT-0800 (PST) //local, pacific | |
moment(m3.toDate()).format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 9:00:00 am" //local, pacific | |
moment(m3.toDate()).tz("America/NEW_YORK").format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 12:00:00 pm" //eastern | |
m3.utc().format('MMMM Do YYYY, h:mm:ss a'); --> "December 31st 2015, 5:00:00 pm" //UTC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment