Khalil
Khalil

Reputation: 119

How to send json Date to Server without conversion - AngularJS/Asp.net Web API

I am working on an Inventory Application (AngularJS/C# WebApi). On the page I have placed the field type="date", which in Chrome browser shows built-in calendar. I am inserting only the date part of the selected date into the database.

However the posted value of the Date field gets changed and it is five hours behind, setting it in the past date, which is not desired, when I will query database for certain date transactions, it will show inaccurate results.

Is there a way where I can send the date value without being affected by conversion. My timezone is (UTC+05:00) Islamabad, Karachi.

Upvotes: 0

Views: 556

Answers (3)

Chris Moutray
Chris Moutray

Reputation: 18349

If its anything like our system - date values will get posted to server as utc+00:00 and should be stored that way in db - when values are sent from server to browser, they're passed as utc+00:00 - the browser converts and you see the date based on clients timezome

Correction when values are sent from server to browser... they are returns as a string in tz format yyyy-mm-ddThh:mm:ssZ - the browser doesn't convert it, you could use js to parse the string to get a date something like this response.mydate = new Date(response.mydate)

In our case seeing the date based on clients timezone wasnt correct eg trying to show London date when use is in China. So we got around this by creating a model object to represent the date without timezone - so you have a field for each date part - day, month and year. We use this for posting date to server and as part of api responses. Could use a angular filter to turn the date-model in to a display value (if its shown as a label) or write special code to map between date input and the model. Its simple and we know exactly how the system will behave regardless of where the user is in the world... So the point is you cant rely on the date and timezone settings of either the server or client machines being set up in a particular way.

Upvotes: 0

Icycool
Icycool

Reputation: 7179

Actually it is correct.

When user select a date, let's say 31 July 00:00 +5 UTC,

you should filter data from 30 July 19:00 +0 UTC to 31 July 18:59 +0 UTC

which is corresponding to user's timezone from 31 July 00:00 +5 UTC to 31 July 23:59 +5 UTC

Upvotes: 1

leonardseymore
leonardseymore

Reputation: 543

Check the timezone settings in your database and operating system the DB is running on

Upvotes: 0

Related Questions