0

I am trying to get a datetimeoffset object from UX to backend api in asp.net core 3.0 hosted on Azure web app.

My model has a datetimeoffset property (lets say ETA). In UX I have a date picker where user can select the date. Once submitted the date will be passed as part of the api body like below

enter image description here

And my model in web api looks like this

enter image description here

All I am doing is saving the date in sql server as a datetimeoffset field. While running locally the updated value in sql is correct like '2020-05-06 00:00:00.0000000 -07:00', while my local machine is in PST. But the moment I deploy to Azure, as its by default treats everything in UTC I believe, the date being saved in DB is '2020-05-06 00:00:00.0000000 +00:00', which is not what I want, I want to save the correct timezone info being sent as part of the api call. My target is users can select eta from around the world, what I need is to save the date along with the correct timezone in sql as datetimeoffset so that all my datetimeoffset compare works fine in backend. Just to add, user is passing a date only but in backend I set the time to 23:59:59 to make the EOD field as true end of day of that specific timezone.

I saw the "WEBSITE_TIME_ZONE" settings but we should not be using it ideally as we have uses all over and app is multi region.

What am I missing here, any direction will be helpful. The api uses the new System.Text.Json library for .net core 3.0. No Newtonsoft.Json library in between. My startup.cs file is default comes with .net core 3.0 template in VS.

SanjayD
  • 156
  • 4
  • 12
  • 1
    You need to get the user's timezone from the browser and pass that along with the picked date. See https://stackoverflow.com/questions/1091372/getting-the-clients-timezone-offset-in-javascript – Ian Mercer May 09 '20 at 00:22
  • You should never assume that the server's timezone is set to anything useful. – Ian Mercer May 09 '20 at 00:23
  • Yes I guess I can use the http interceptor to send the timezone for every request as part of http header like, 'user-local-timezone-offset': new Date().getTimezoneOffset().toString(). But I was looking for some way of handling datetime with timezone between UX & API automatically I guess :) Thanks for the details. – SanjayD May 09 '20 at 00:42

0 Answers0