24

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.

Community
  • 1
  • 1
KVISH
  • 12,923
  • 17
  • 86
  • 162
  • 6
    If you're going to down vote please explain why. I have clearly tried many things for this. – KVISH May 30 '16 at 22:48

2 Answers2

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

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • hi @Matt mon problem is that I have not the input date in string format, but in UTC, and moment or any function can convert it to string, i get always convert to stringl like Wed Nov 01 2017 12:22:04 GMT+0100 (CET), any ideas? – stackdave Nov 02 '17 at 06:33
  • @stackdave - I don't understand your comment. It appears to be unrelated to the question here. Please ask in a new question with supporting details. – Matt Johnson-Pint Nov 02 '17 at 17:27
  • After two days of struggle. This solution worked for me. Thank you so much @MattJohnson-Pint – Bhuvan Arora Nov 27 '20 at 12:54
  • TypeError: moment_1.default.tz is not a function `const input = moment(req.query.created_at).format("YYYY-MM-DD h:mm:ss A");` `const clientDate = moment.tz(input, "YYYY-MM-DD h:mm:ss A", timezone).utc().format("YYYY-MM-DD h:mm:ss A");` – Tirath Sharma May 25 '23 at 05:28
  • What if I am getting input date from client, How can I know format than than. – Tirath Sharma May 25 '23 at 05:30
  • @TirathSharma - if it's _arbitrary_ input, then you can't. For example, if I give you `01/02/03`, that could be any of `DD/MM/YY`, `MM/DD/YY`, or `YY/MM/DD` format - there's no way to know from the data alone. – Matt Johnson-Pint May 29 '23 at 18:11
2

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

nbs
  • 311
  • 1
  • 12