Eric B.
Eric B.

Reputation: 24411

Any way to parse a time string using Moment.js but ignore timezone info?

Given the volume of Timezone questions, I would have thought to be able to find the answer to this issue, but haven't had any success.

Is there a way using moment.js to parse an ISO-8601 string but have it parsed in my local timzeone? Essentially I want to ignore the timezone information that is supplied in the ISO string.

For example, if I am in EDT timezone:

var x = moment( "2012-12-31T00:00:00+0000" );

will give me: "2012-12-30T19:00:00-5000"

I'm looking to ignore the timezone info and just have it give me a moment equivalent of "2012-12-31T00:00:00-5000" local time (EDT).

Upvotes: 21

Views: 35918

Answers (10)

Fawaz
Fawaz

Reputation: 3560

Use moment.parseZone to convert without taking into account the timezone.

const moment = require('moment')

const dateStr = '2020-07-21T10:00:00-09'

const date = moment.parseZone(dateStr)

console.log(date.format('MM-DD-YY HH:mm A')) // 07-21-20 10:00 AM

Try here link to docs

Upvotes: 0

Steven
Steven

Reputation: 3813

You can ignore the browser's timezone completely by creating a new moment using moment.utc() instead of moment().

For example, if you are trying to work purely with a UTC date/time of the browser's current time but want to discard its timezone data, you can recreate the browser's current time into a UTC format using the following:

let nowWithTimezone = moment();
let nowInUtc = moment.utc(nowWithTimezone.format('MM/DD/YYYY HH:mm'), 'MM/DD/YYYY HH:mm');

Further documentation on moment.utc(): https://momentjs.com/docs/#/parsing/utc/

Upvotes: 4

frustigor
frustigor

Reputation: 402

Momentjs default logic will format the given time with local timezone. To format original date, I wrote a function:

https://github.com/moment/moment/issues/2788#issuecomment-321950638

Upvotes: 0

pmont
pmont

Reputation: 2129

There are valid reasons to do what the OP is asking for. The easiest way to do this with Moment is using its parseZone(date) method. No futzing around with string manipulation or multiple calls. It effectively parses the date string as though it were in the browser's local time zone.

Upvotes: 1

Mark
Mark

Reputation: 1680

I solved this by supplying a format as the second argument, and using Moment's method of escaping characters, and wrapped square brackets around the timezone.

moment("2016-01-01T05:00:00-05:00", "YYYY-MM-DDTHH:mm:ss[Z]").startOf("hour").format()

This will still create moment objects using your local time zone, but it won't do any sort of auto-timezone calculation. So the above example will give you 5am regardless of timezone supplied.

Upvotes: 14

vrtis
vrtis

Reputation: 747

I know I'm late to the party, I had the same question and my searches didn't bring me any closer. I broke down and read the documentation and there is an option in moment for a String + Format:

String + Format docs

moment(String, String);

moment(String, String, String);

moment(String, String, Boolean);

moment(String, String, String, Boolean);

and more words, then this:

Unless you specify a time zone offset, parsing a string will create a date in the current time zone.

moment("2010-10-20 4:30",       "YYYY-MM-DD HH:mm");   // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC

The part that gave me pause was the example that was used to parse local time omitted the +0000, which lead me to think the input string needed to have that removed, but it doesn't.

example:

var time = "2012-12-31T00:00:00+0000";
var x = moment(time); // Sun Dec 30 2012 19:00:00 GMT-0500
var y = moment(time,'YYYY-MM-DD'); //Mon Dec 31 2012 00:00:00 GMT-0500

Upvotes: 7

Thomas Modeneis
Thomas Modeneis

Reputation: 697

This is difficult task to do with MomentJS, it will basically depend as well on your current timezone.

Documentation as well is vague for this specific task, the way I solved the issue on my side was by adding hours to the date before converting it to JSON format.

var dt = moment("Sun Sep 13 2015 00:00:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400", false);
var date = dt.add(2, 'hour').toJSON();
console.log(date); //2015-09-13T00:00:00.000Z

Upvotes: 0

Thomas Modeneis
Thomas Modeneis

Reputation: 697

The best way is to use:

dt = moment("Wed Sep 16 2015 18:31:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400",true);

And to display convert again to desired timezone:

 dt.utcOffset("-04:00").toString()
 output > Wed Sep 16 2015 18:31:00 GMT-0400

Upvotes: -1

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241505

I don't think you really want to ignore the offset. That would ultimately just be replacing the offset you provided with one from your local time zone - and that would result in a completely different moment in time.

Perhaps you are just looking for a way to have a moment retain the time zone it was given? If so, then use the moment.parseZone function. For example:

var m = moment.parseZone("2012-12-31T00:00:00+0000");
var s = m.format();   // "2012-12-31T00:00:00+00:00"

You could also achieve this with moment.utc. The difference is that moment.parseZone will retain whatever offset you give it, while moment.utc will adjust to UTC if you give it a non-zero offset.

Upvotes: 14

khalid13
khalid13

Reputation: 2837

If you know for sure your input string is in the ISO-8601 format, you could just strip off the last 5 digits and use that in the Moment constructor.

var input = "2012-12-31T00:00:00+0000"
input = input.substring(0, input.length-5)
moment(input).toString()
> "Mon Dec 31 2012 00:00:00 GMT-0600"

Upvotes: 3

Related Questions