Created
October 2, 2012 15:12
-
-
Save dresende/3819961 to your computer and use it in GitHub Desktop.
Using momentjs and node-time, having an UTC date, format it in any timezone
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
# this was runned on the first dates below (Oct 2 2012, 3:10 PM UTC) | |
UTC: Tuesday, October 2 2012 3:10 PM | |
LX: Tuesday, October 2 2012 4:10 PM | |
NY: Tuesday, October 2 2012 11:10 AM | |
TKY: Wednesday, October 3 2012 12:10 AM | |
3 months later... | |
UTC: Wednesday, January 2 2013 3:10 PM | |
LX: Wednesday, January 2 2013 3:10 PM | |
NY: Wednesday, January 2 2013 10:10 AM | |
TKY: Thursday, January 3 2013 12:10 AM | |
7 months before... | |
UTC: Friday, March 2 2012 3:10 PM | |
LX: Friday, March 2 2012 3:10 PM | |
NY: Friday, March 2 2012 10:10 AM | |
TKY: Saturday, March 3 2012 12:10 AM |
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
var moment = require("moment"); | |
var time = require("time"); | |
var now = moment.utc(); | |
// moment plugin to simplify timezone shifting.. | |
moment.fn.shift = function (tz) { | |
var zone = new time.Date(this.valueOf()); | |
zone.setTimezone(tz.replace(" ", "_")); | |
return moment.utc(this.valueOf()).add("minutes", -zone.getTimezoneOffset()); | |
}; | |
console.log("UTC:", now.format("LLLL")); | |
console.log(" LX:", now.shift("Europe/Lisbon").format("LLLL")); | |
console.log(" NY:", now.shift("America/New York").format("LLLL")); | |
console.log("TKY:", now.shift("Asia/Tokyo").format("LLLL")); | |
now = now.add("months", 3); | |
console.log("\n 3 months later...\n"); | |
console.log("UTC:", now.format("LLLL")); | |
console.log(" LX:", now.shift("Europe/Lisbon").format("LLLL")); | |
console.log(" NY:", now.shift("America/New York").format("LLLL")); | |
console.log("TKY:", now.shift("Asia/Tokyo").format("LLLL")); | |
now = now.subtract("months", 10); // 10 = 7 months before + 3 months already added | |
console.log("\n 7 months before...\n"); | |
console.log("UTC:", now.format("LLLL")); | |
console.log(" LX:", now.shift("Europe/Lisbon").format("LLLL")); | |
console.log(" NY:", now.shift("America/New York").format("LLLL")); | |
console.log("TKY:", now.shift("Asia/Tokyo").format("LLLL")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment