Search code examples
c#asp.net-mvc-3dateutc

How to display date/time client-side in user's time zone without using JavaScript?


Lets say I have the following DateTime: DateTime.UtcNow, and I want to display it on the client according to the user's time zone, and without recurring to JavaScript.

How should I be doing this? I guess there would be somewhere I should get the time zone for the current request from, somehow, based on the user IP or something?


Solution

  • Using an IP address to physical address conversion service is the only 'automatic' and reliable alternative to JavaScript that I'm aware of. Depending on how accurate you want the IP address to physical location conversion to be you may need to turn to a paid service (I'm thinking if the user is near a timezone boundary you might place them incorrectly).

    I've also come across free downloadable databases that match IP address ranges to countries. But these do change from time to time and can be out of date. I would provide a link but it was a while ago that I found this. Hopefully it's helpful just knowing this sort of thing is out there.

    You should consider that maybe your users want to manually set their preferred time zone in cases where they travel but still want to see the same times (a situation I've come across at work).

    Just keep in mind that JavaScript is the easiest solution and most reliable solution so if you can get away with using it... go for it

    Update:

    I've found a similar question here that pretty much covers it too How to get current user timezone in c#