Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs convert timestamp string into date with custom timezone and to UTC

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

like image 504
Pavel 'PK' Kaminsky Avatar asked Mar 16 '26 11:03

Pavel 'PK' Kaminsky


1 Answers

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
like image 162
Pavel 'PK' Kaminsky Avatar answered Mar 17 '26 23:03

Pavel 'PK' Kaminsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!