1

I am planning a .NET application that will be used in different countries. This application contains a web API and a single page front end. The application will also use a database table with a datetime column. The values in the table will be for the UTC timezone.

I am confused about handling these values. My users may be in any country. Where can I convert the local time to UTC time? Database data goes via an API to the front end application. User data comes from a single page application to the database via web API.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
barteloma
  • 6,403
  • 14
  • 79
  • 173
  • Have a local time zone setting in the user preferences? Or in the client javascript has `getTimezoneOffset()` so your view can do the conversion – Dave S Nov 29 '22 at 19:58
  • .Net partly handles this for you by inferring localization settings from the browser session. That is, a value retrieved from the database will likely be converted to local time automatically on the web page. However, this detection is not perfect and the web API may throw a wrench in the works here. For the other direction (user -> database), you will also have to pay attention to the `.Kind` property in your C# DateTime values. But again: there's enough going on you'll want to check everything carefully. – Joel Coehoorn Nov 29 '22 at 20:31
  • 1
    @DaveS - That's an offset only, which is often insufficient. Use the resolved time zone identifier, [as shown here](https://stackoverflow.com/a/34602679/634824). – Matt Johnson-Pint Nov 29 '22 at 20:58
  • 1
    @JoelCoehoorn - .NET only infers the locale (such as `en-US`). It will not infer the time zone (such as `America/Los_Angeles`). They are orthogonal concepts. – Matt Johnson-Pint Nov 29 '22 at 20:59
  • Even more to @MattJohnson-Pint comment - the information about time zone is not sent from browser to server, so (unlike accept-language header) the only thing server can do (without roundtrip to browser) is to guess timezone by IP... which is as good as any other guesses (not good at all). – Alexei Levenkov Nov 29 '22 at 21:16

3 Answers3

2

For the scenario you described, the best practice would be to keep the values in terms of UTC as they are returned from your API. For example, your API may return a result such as:

{
  "id": 123
  "timestamp": "2022-12-31T23:59:59.9999999Z"
}

The timestamp in that result is in ISO 8601 extended format (and is also RFC 3339 compliant). The Z at the end means the timestamp is in terms of UTC.

In the browser, you can use JavaScript to convert that timestamp to the user's local time. There are a large number of ways to do that, but the simplest would be:

new Date(timestamp).toString()

If you need to control formatting or convert to a time zone other than the user's local time zone, consider toLocaleString instead, or a JavaScript library such as Luxon or date-fns.

Alternatively, if your API must return local time values, then you will need to use the .NET TimeZoneInfo class to perform conversions (or NodaTime). You'll also need to know the time zone identifier for the user's browser, which you can get in JavaScript with the Intl API, as shown here.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

DateTime in C# as function like ToUniversalTime() and ToLocalTime(). See https://learn.microsoft.com/en-us/dotnet/api/system.datetime.touniversaltime?view=net-7.0

Vic F
  • 1,143
  • 1
  • 11
  • 26
  • 1
    Neither of those functions are suitable for a web application, as they convert to/from the *server's* local time zone setting - not the user's. – Matt Johnson-Pint Nov 29 '22 at 21:06
  • At least it is upvoted to troll OP :) As @MattJohnson-Pint pointed out these functions are absolutely not useful for the case OP is asking about since timezone information is not available on the server where C# code runs normally. Answer needs a lot of clarification to explain how these two methods help to format datetime values in C# code according to user's/browser's local time zone which is only available in the browser. – Alexei Levenkov Nov 29 '22 at 21:12
0

You are on the right path. There are several principles when dealing with DateTime.

  1. You always store DateTime value in the UTC time zone to the database.
  2. On your API, you always ask for DateTime in UTC time zone and return DateTime in UTC time zone. You can oblige a consumer of your API to send you in date time format which includes time zone, and of course, you can guarantee that you always return a DateTime in UTC time zone.
  3. On the presentation layer, in your Single Page Application, you convert fetched date time from API to user local time.

P.S. It was on purposely mentioned word UTC so many times. UTC is a measurement unit, like see level for altitude.

Ognjen Stanić
  • 505
  • 8
  • 17