Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

momentjs convert datetime to UTC from another timezone

Tags:

I'm having dates like the below:

05/30/2014 11:21:37 AM

My users will be entering in that data and having their own timezone. It could be anything like "US/Eastern", "US/Pacific-New", etc. I want to convert the time to UTC but i'm not able to. Is there a way to do so?

I'm using node and I tried momentJS and read the below:

http://momentjs.com/docs/

http://momentjs.com/guides/

Convert date to another timezone in JavaScript

How do I convert from a different timezone to UTC?

Edit

I have tried these:

moment().utc(0).format('YYYY-MM-DD HH:mm Z') moment.tz(dateString, "US/Eastern").format() 

In the above example dateString is the string date. I want to set the timezone to "US/Eastern" and convert it to UTC.

like image 618
KVISH Avatar asked May 30 '16 22:05

KVISH


People also ask

How do I change timezone in MomentJS?

To change the default time zone, use moment. tz. setDefault with a valid time zone.

How do you convert date to UTC format?

To convert a JavaScript date object to a UTC string, you can use the toUTCString() method of the Date object. The toUTCString() method converts a date to a string, using the universal time zone. Alternatively, you could also use the Date. UTC() method to create a new Date object directly in UTC time zone.

What is moment tz ()?

For example, if you parse the string '2020/01/02' and then call the moment#tz() method, you're telling moment to parse the string in the locale time, and then convert the date to a given IANA timezone.


2 Answers

// your inputs var input = "05/30/2014 11:21:37 AM" var fmt   = "MM/DD/YYYY h:mm:ss A";  // must match the input var zone  = "America/New_York";  // construct a moment object var m = moment.tz(input, fmt, zone);  // convert it to utc m.utc();  // format it for output var s = m.format(fmt)  // result: "05/30/2014 3:21:37 PM" 

Note that I used the same output format as input format - you could vary that if you like.

You can also do this all in one line if you prefer:

var s = moment.tz(input, fmt, zone).utc().format(fmt); 

Additionally, note that I used the Area/Locality format (America/New_York), instead of the older US/Eastern style. This should be prefered, as the US/* ones are just there for backwards compatibility purposes.

Also, US/Pacific-New should never be used. It is now just the same as US/Pacific, which both just point to America/Los_Angeles. For more on the history of this, see the tzdb sources.

like image 137
Matt Johnson-Pint Avatar answered Sep 20 '22 15:09

Matt Johnson-Pint


Very similar to @Matt's answer; but using moment-timezone to convert date-time to UTC (as ISO string):

var momentTz = require('moment-timezone'); var utcDate = momentTz.tz('2021-01-27 14:02:45', "YYYY-MM-DD HH:mm:ss", 'Asia/Kolkata').toISOString(); console.log(utcDate);  // output: '2021-01-27T08:32:45.000Z' 

The api I was working with required date-time in UTC & in ISO format

like image 40
nbs Avatar answered Sep 20 '22 15:09

nbs