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..

5
  • you requirement is getting time from client and trust that instead of using server time? Commented Mar 11, 2014 at 13:42
  • requirement is get user's date in 'YYYY-MM-DD hh:mm:ss' format, convert it to a UTC date according user's timezone Commented Mar 11, 2014 at 13:45
  • Are you collecting the input in a web browser? Why not just convert to UTC there, so you don't have to involve time zones at all? Why does it have to be done on in node.js server side code? Commented Mar 11, 2014 at 15:12
  • 1
    because it's not a browser, it's a legacy code on hardware which cannot be modified and always set to a particular and fixed TZ. Commented Mar 12, 2014 at 11:01
  • Related: How to initialize javascript date to a particular timezone Commented Mar 13, 2014 at 16:33

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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");

2 Comments

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?
you check, preload a table with time diff. select the one you need at runtime. eu timezone is not that many
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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.