0

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.

3 Answers 3

1

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

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

6 Comments

Insofar the filtering is concerned I think this should work as I have done the most part of the data entry and do not want to alter the posting model of each form. However, one thing is making me worry that when the data is loaded back, those dates are not converted back to show what I have posted/selected.
hmm... if you are using normal javascript date, it should convert the time back to user's timezone. If you are using server side rendering then you have no way to know user's timezone.
I am communicating with ASP.NET Web Api JSON response through AngularJS $http service.
Then new Date using the value should be fine.
Cool, tested it in the console windows and it is correct. Do you recommend that I should filter the database records with two dates parameters?
|
0

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

4 Comments

The ajax post request convert the value before it reach the server. So there is no problem with the Database. I just want the date to reach server as it is.
I see. The only way I know of is to convert it to epoch (UTC 0) new Date().getTime(). Then on your server convert this number back to a date object
This makes sense, however, I dont want to mess with the server code at this stage where a lot of work has already been done.
Ok, maybe try this new Date(date.valueOf() + date.getTimezoneOffset() * 60000);
0

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.

3 Comments

Appreciated. but lets say I posted 28th of July 2017 with hours component to 0, the ajax posted it as 27th of july with hours 1900, when it loaded back on the browser it was not converted back to 28th July 2017 and stayed the saved version
@Khalil i see you sorted you problem - thought i'd amend my answer in case it gives you a different insight
Yes it was kind of work around, even then It was not going to solve my issue completely as in the Database the field type is just 'date', I was loosing time value anyway and it the reverse conversion was out of equation. I have override the Date.prototype.toISOString() to return same date formatted in json

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.