Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

momentjs toISOString without the "z"

I need to create ISO-8601 date. With momentjs I'm using

moment(my_date).toISOString() to create an ISO date, the result is similar to this:

2015-03-17T15:12:38.076Z

I need to send this date to an API which expects the date a little different (although it's documentation says they're using ISO-8601):

2015-03-17T15:12:38.076-4:00 

Is there a way to get the expected formatting using momentjs or something else?.

Edit: I found a solution:

moment.tz(my_date, "America/Argentina/Buenos_Aires").format("YYYY-MM-DDTHH:mm:ss.SSSZ")
like image 359
leonardorame Avatar asked Mar 17 '15 15:03

leonardorame


1 Answers

You can use momentjs timezone: http://momentjs.com/timezone/

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
london.format();     // 2014-06-01T17:00:00+01:00

The z inidcates an UTC timestamp, the API is expecting the difference to UTC and therefor the -4:00. If you do want conversions between timezones momentjs timezone is my suggested way to go.

But doesn't moment().format(); returns the time as 2014-09-08T08:02:17-05:00 ?

like image 89
Sven 31415 Avatar answered Oct 01 '22 03:10

Sven 31415