Reputation: 25
I am facing a weird scenario that the date of a textbox (which is fetched by sql database) is changed randomly by date (-1)day
My WebApp is deployed in USA on two servers *(the both servers have time difference on 1 hour due to UTC-5, UTC-6) and i am in Timezone UTC+5.
Inside SQL Table there is a date '05/31/2020'
but sometimes it shows '05/30/2020'
on front end.
The column type in SQL is Date. Then i get it DataSet, and on code behind this line is used Convert.ToDateTime(DataSet.Tables[0].Rows[0]["Date"])).ToShortDateString()
When i debug this code locally the date is correct every time.
Upvotes: 2
Views: 237
Reputation: 9209
I suspect that somewhere you have an issue with local and UTC time.
With you on UTC+5 and your servers on UTC-5 and UTC-6 its possible for them to be a day behind you e.g. your local time is before 10am Friday they will still be on Thursday.
How does your back end handle the date? It should always be in UTC, you then load your SQL DATE value into a DateTime type and use DateTime.ToLocalTime to convert to your UI and DateTime.ToUniversalTime when passing from UI to back end SQL.
Upvotes: 2