It is necessary to format the time zone offset using js

I have a date picker on my page when I select any date when it gives a result similar to

Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)

And I need to format it: YYYY-MM-DDTHH:mm:ss Z

So, for this I use the syntax of the moment

var date='Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)';
moment(date).format('YYYY-MM-DDTHH:mm:ss Z');

which produces output

2014-09-07T00:00:00 +05:00

This is good, but my api expects a standard timezone offset of "Z" instead of playing out the local current timezone (like +5: 00) in my case.

So I want to create this

2014-09-07T00:00:00Z

How is this possible?

+4
source share
2 answers

Use moment.utc () to display UTC time instead of local time:

var dateValue = moment(date).utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';

moment().toISOString() ISO (: YYYY-MM-DDTHH:mm:ss.sssZ, UTC):

var dateValue = moment(date).toISOString();

JSFiddle

+11

, "Z" , toJSON(), js v2.0. :

moment('Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)').toJSON();

JSFiddle.

+1

All Articles