3

Suppose i get the value '2014-03-03 16:20:20' as an input, I'd like to convert it to it's real time with timezone (i.e, node sits in Europe , but timestamp was collected in America), and after that I'd like to get the UTC representation of the real value.

Any idea what packages can help? Tried moment.js with no luck..

Pavel 'PK' Kaminsky
  • 796
  • 1
  • 10
  • 22

4 Answers4

2

finally found the answer with momentjs-timezone ,

which is kinda funny since you wont get a event a hint about on the mainsite http://momentjs.com/timezone/ but you can find it on http://github.com/moment/moment-timezone

anyhow here's how you do it :

var tz0 = moment.tz("2014-03-03 16:20:20", "Asia/Jerusalem")
var tz1 = moment.utc(tz0);


console.log(tz0.format('YYYY-MM-DD hh:mm:ss'));
console.log(tz1.format('YYYY-MM-DD hh:mm:ss'));

and the output as you can see respectivly

2014-03-03 04:20:20
2014-03-03 02:20:20
Pavel 'PK' Kaminsky
  • 796
  • 1
  • 10
  • 22
0
// moment.js

var utc = moment('2014-03-03 16:20:20','YYYY-MM-DD HH:mm:ss', 'us');
// or timezone string pass from client

var eu = utc.zone("+02:00");
var eu = utc.zone("-02:00");
wayne
  • 3,410
  • 19
  • 11
  • what if i don't know the zone time difference ("+02:00")? what if i want to pass the "us" time and convert to local, and then to UTC? – Pavel 'PK' Kaminsky Mar 11 '14 at 14:06
  • you check, preload a table with time diff. select the one you need at runtime. eu timezone is not that many – wayne Mar 11 '14 at 14:11
0

momentjs may help you. Like the following:

  1. Could you format the input with ISO date plus timezone info, like 2014-03-03T16:20:20-05:00 (supposing you get from local time at GMT-5 timezone)
  2. Then call: moment.parseZone('2014-03-03T16:20:20-05:00').zone(1).format() (supposing you want to get local time at GMT+1 timezone)

Hope that help, Ron

Ron
  • 6,037
  • 4
  • 33
  • 52
0

Timezone is an elegant module for this.

var tz = require("timezone/loaded");
var formattedDate = tz(unformattedTimestamp, '%d %B %Y, %-I:%M %p', "Europe/Amsterdam");

You can use bootstraphelper to capture user's timezone.

Avinash
  • 779
  • 7
  • 18