1

I am having one website in which it has functionality to Login with specific timezone regardless what's the timezone on client side.

Now, In website when user selects a date in dialog.I am sending it to server side using JSON.stringify with several other properties.

But whenever it is received at server side date is changed.

Example :-

I logged in using (+05 : 30) India time zone "01/08/2015 00:00:00" and server is having Casablanca Timezone.

When the date is received at server side the date is reduced by One "31/08/2015".

I think it is because of timezone conversion.

I already checked following link :-

JSON Stringify changes time of date because of UTC

I already tried that answer :- https://stackoverflow.com/a/1486612/2592727

But i am unable to understand how that formula works. So it's better to get more details and work with some specific solution.

Requirement :-

I am allowing user to select only date. I want same date to be received over server side.

How can i accomplish this? Please describe with more details if possible.

Is there any simple way to avoid this collision?

asked Aug 7, 2015 at 12:17
5
  • 2
    Just make sure that your date value is a string object so JSON.stringify would not touch it with is dirty paws. Commented Aug 7, 2015 at 12:30
  • I dont want to pass it as string. Thanks. But please any other solution. Commented Aug 7, 2015 at 13:01
  • 1
    Why not, the database is going to mind it. Commented Aug 7, 2015 at 13:03
  • @Mouser. I am using ASP.Net MVC. when i pass date as string i use DateTime as datatype in my model class. So implicitly it is unable to convert string in to DateTime and gives false date. Commented Aug 7, 2015 at 13:19
  • @Mouser your solution is good. But i also want to learn little bit about handling this timezone difference. Commented Aug 7, 2015 at 13:35

2 Answers 2

1

The JSON.stringify method stored the date as UTC in the string, and when you parse that in .NET you will get a DateTime that contains the exact same point in time, but converted to the local time of the server.

You can handle this in two ways, you can either convert the time back to the original time zone, or you can avoid using the Date data type.

Converting to the original time zone of course requires you to know what the time zone is. Example:

var zone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(date.ToUniversalTime(), zone);

To avoid using the Date type, you would format the date into a string before serialising it. Example:

var s = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();

On the server side you would parse the string using the same format:

DateTime userTime = DateTime.ParseExact(date, "yyyy-M-d", CultureInfo.InvariantCulture);
answered Aug 7, 2015 at 12:51
1

Use the format yyyy-mm-dd hh:mm:ss and you will never get this ever again, regardless of timezone

answered Aug 7, 2015 at 12:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.