1

We have a DateTime column in a table wherein we store date selected in a dropdownlist from the UI

enter image description here

we are saving it with the default DateTime.Unspecified

enter image description here

So in the database it stored like this

enter image description here Now the problem is we want to display this in the client as it is, meaning display this datetime as it is regardless of timezones

So currently what we did is just get the datetime value from the database and trim the 00:00:00, but somehow, we are encountering problem that it displays differently in clients in different timezone like for example in -2 Timezone, it displays one day behind.

Any idea how should we approach this? changing the database column to datatimeoffset is not an option for this. We just want to force it to be displayed as it is.

Linc Abela
  • 797
  • 2
  • 12
  • 26

1 Answers1

1

The DateTime class has some property that can adjust the display like :

DateTimeKind.Utc

or

DateTime d =  DateRetreived.ToUniversalTime()

In your case, I think you just need to put your dates in UTC format before sending it in the display.

See this question :

How can I format DateTime to web UTC format?

Documentation :

https://msdn.microsoft.com/en-us/library/system.datetime.specifykind(v=vs.110).aspx

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • What about the existing records that already saved DateTimeKind.Unspecified? how do I retrieve it as it is? – Linc Abela Aug 29 '17 at 02:22
  • Something like `DateTime varDisplayed = new DateTime( OC.DateCompleted, DateTime.Utc);` ... Is `OC` your database object ? – Antoine Pelletier Aug 29 '17 at 03:00
  • Also, would you like to change them in the back end C# code BEFORE sending them to web page, or you are looking more for some code AFTER sending it to web front end, like html and javascript ? – Antoine Pelletier Aug 29 '17 at 03:06