Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MomentJS toISOString with local timezone

Tags:

momentjs

I've been using toISOString to serialize momentJS date before sending to the server via jQuery. It works fine for me except it converts date to UTC but I need to keep the local timezone. Does momentJS have a method for this?

like image 706
SiberianGuy Avatar asked Oct 12 '14 17:10

SiberianGuy


People also ask

How do I get local timezone in Momentjs?

var tz = moment. tz. guess(); It will return an IANA time zone identifier, such as America/Los_Angeles for the US Pacific time zone.

How do I change timezone in Momentjs?

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

Is toISOString a UTC?

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

Is Momentjs deprecated?

Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.


2 Answers

You can simply call .format() without any parameters.

like image 96
Matt Johnson-Pint Avatar answered Sep 19 '22 15:09

Matt Johnson-Pint


Added in 2.20.0:

moment().toISOString(true)

Output:

"2018-05-13T16:16:13.507+03:00"

Found answer here.

Documentation.

like image 37
Shahin Dohan Avatar answered Sep 19 '22 15:09

Shahin Dohan