public
Created

Using momentjs and node-time, having an UTC date, format it in any timezone

  • Download Gist
output.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 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
tz.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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"));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.