Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent ASP.NET MVC from converting date from UTC to local?

I store dates in the database using UTC. The client (javascript) gets the date and converts it into local time. This works fine when running on localhost (meaning web server time zone is same as client time zone). However, when I deploy to Azure, where the web server time zone is UTC, I get back the date already adjusted to the client's time zone, causing the client to show the wrong date time value.

I am assuming that ASP.NET MVC does this conversion. Is that true? If so, how can I prevent it, and force the dates to be passed down unaltered?

I have tried to set the kind of the datetime object being passed down to DateTimeKind.UnSpecified but that does not work.

like image 270
floatingfrisbee Avatar asked Nov 14 '22 15:11

floatingfrisbee


1 Answers

If you store the value as UTC and send it down as UTC, the web server's clock doesn't come into play. You need to check your how that date is getting set from the db, if that is good, then check the conversion happening in JavaScript.

Also, if you are doing any .ToString() or using HtmlHelpers, they can also convert on the fly.

like image 104
rick schott Avatar answered May 09 '23 10:05

rick schott