Arat Kumar rana
Arat Kumar rana

Reputation: 187

json date issue in jquery

I have a input field fromDate on focusing on the input field it will open a calendar to select the date and after selecting the date I want to convert the date to json date. For e.g I select the date as 21/03/2014 but while converting it to json date it is taking as 20th March 2014:

Below is my code

var fromDate = $("#fromDate").datepicker("getDate");
alert(fromDate);    
var dd = fromDate.toJSON();
alert(dd);

the alert from date is showing as Fri Mar 21 2014 00:00:00 GMT+0530(India Standard Time)

and after converting it to fromDate.toJSON() it is showing as 2014-03-20T18:30:00:000Z

But after converting to json date I want it should show 21st, could you help me why it is taking previous date and please tell me the solution

Upvotes: 1

Views: 131

Answers (2)

Jay Bhalodi
Jay Bhalodi

Reputation: 387

according to tvanfosson that will take UTC format. so you can incrise your date by 5:30 like in your example.

var fromDate = $("#fromDate").datepicker("getDate");
alert(fromDate);
var json_date = new Date(fromDate.getTime() + (5.5 * 60 * 60 * 1000));
var dd = json_date.toJSON();
alert(dd);

hope this works.

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532435

The date is normalized to UTC. That time represents the same time in UTC as in your timezone. When you convert it back to your timezone (during deserialization) it should be correct.

Upvotes: 0

Related Questions