1

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But if I deploy this code in another country(USA) and they use the application on USA timezone, then it will not work. I want to globalize it, so whereever we browse it should show the same date as the input date. Could you help me?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.