I am using asp.net c#. When the user perform an action I am saving the action datetime in the db using UTC time. Now when its time to present the actions, I am sending json to client with the datetime from the db. Now the problem is that it is being present in utc time and that is really confusing to the user. What are the optional solutions to my issue? should they be client based or server based?
-
We will need to see a re-create: please 1. make it minimal, 2. include server side code that sends the time to the client, and 3. any code on the client that interacts with the date before being shown. – Richard Feb 02 '16 at 11:11
-
Also provide information how do you send this JSON. Do you use ASP.NET (MVC/WebAPI) or other web framework? – Marcin Zablocki Feb 02 '16 at 11:13
-
3This doesn't sound like a C# issue at all - it sounds like a presentation-logic issue. Sending the UTC time to the client is probably the right thing to do, and then you can convert it to local time client-side, in Javascript. – Jon Skeet Feb 02 '16 at 11:14
3 Answers
I answered this exact question here:
How to get current user timezone in c#
Basically, in your database, you should store the time in UTC. Then, you need to get your JavaScript to call your WCF web service, and pass it the user's local timezone.
Your service can add this timezone-offset to the UTC time, and return back the date-times in the user's local time.

- 1
- 1

- 27,846
- 7
- 149
- 159
I think that the most correct way is to keep sending the data to the client the same way you do now (in UTC), then you will be able to convert it to Local or any other timezone as you wish during the presentation, but keep using the UTC in calculations if any.
There are multiple questions on how to do that in javascript, e.g. here
Append 'UTC' to the string before converting it to a date in javascript:
var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
P.S. Not sure you can do various time-zones in js, but at least it should work with the local one

- 1
- 1

- 27,817
- 27
- 121
- 207
Convert the UTC time to local time based on the time zone of the client. If that is done server-side or client-side, is a matter of design, logic/presentation layer separation.
If you choose to do it server side, you will need the information of the client's local time zone available. Given that, you can write:
TimeZoneInfo.ConvertTimeFromUtc(utcdate, clients_timezoneinfo)
Beyond the c# part, there is good coverage on time zone offsets in client browsers, for example get client time zone from browser

- 1
- 1

- 8,493
- 3
- 36
- 77