Rocky Singh
Rocky Singh

Reputation: 15430

ISO date convert issue

I have the following date:

new Date('Sun Jun 30 2013 00:00:00 GMT+0530 (India Standard Time)')

I want to convert it to toISOString, for that I tried the following code:

new Date('Sun Jun 30 2013 00:00:00 GMT+0530 (India Standard Time)').toISOString()

but it gives me the following output:

"2013-06-29T18:30:00.000Z"

I want the following output:

  "2013-06-30T00:00:00.000Z"

i.e ignoring the local time offset.

Upvotes: 1

Views: 2857

Answers (4)

Code Adda
Code Adda

Reputation: 1

If you are using moment library try this :

moment.utc(new Date().toISOString()).format();

Upvotes: 0

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241495

Since you started with a value at +5:30, then it makes perfect sense that the resulting value is at 18:30 UTC on the prior day. It had to subtract 5 hours and 30 minutes to determine the UTC time.

You say you want the value back as 2013-06-30T00:00:00.000Z - but you must understand, that would be a completely different moment in time. The Z is not just there to be pretty - it specifically indicates that the time is at UTC.

If you wanted the value in ISO format in the original time zone, then you specify the time zone offset as part of the string, such as 2013-06-30T00:00:00.000+05:30.

Now, you should also understand that support for parsing date strings, as you did in your first line of code, is highly dependent on the browser. Different browsers have support for different string inputs, but not others. You may find that using the input value you did is not going to work everywhere.

Even .toISOString() is not supported by all browsers. It is there in most newer browsers, but not everywhere. Even where it is available, it will always return the ISO string at UTC, and not in the extended format I showed above. You can read this post for options to get the full extended ISO string.

Upvotes: 2

whizzzkid
whizzzkid

Reputation: 1295

you can basically balance out the 5:30 hours being subtracted by

//create date object
var date = new Date('Sun Jun 30 2013 00:00:00 GMT+0530 (India Standard Time)');
//add 330(5:30 hrs) mins in ms 
date.setTime(date.getTime() + (330 * 60 * 1000));
//output in iso format
var ans  = date.toISOString()

Upvotes: -1

emaniacs
emaniacs

Reputation: 137

I not really sure about this, but if you want the output like that, i try to change the code like this.

new Date('Sun Jun 30 2013 00:00:00 GMT+0530 (India Standard Time) UTC')

toISOString

Upvotes: 0

Related Questions