0

I have saved a utc datetime in database and I have to display the datetime value in local time of the user`s country setting.

There are multiple ways to get a local datetime from a utc datetime:

var dateTimeLocal = TimeZone.CurrentTimeZone.ToLocalTime(dateTimeUtc);
var dateTimeLocal = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"));
var dateTimeLocal = dateTimeUtc.ToLocalTime())

It seems most time there is a timezone involved to get the local date because the timezone determines the offset in hours to the local datetime.

When I have a user.country = "SE"(Sweden), how do you programatically determine the timezone to convert the correct local datetime from the utc datetime?

Or is it enough to set the CurrentThread Culture/UiCulture to "sv-SE" and the timezone is automatically set by the .NET framework?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • 1
    Countries can have more than one timezone. So you cannot get a timezone just by passing in a country. If you set the thread CurrentCulture, you can use the DateTime class in .NET to do the conversion from UTC to local. – Jon May 03 '16 at 20:19
  • You have to use the second option. Also see here: http://stackoverflow.com/questions/7977736/get-timezone-by-country-and-region – bns May 03 '16 at 20:19
  • @bns No duplicate, because your link does lead to no solution. Just some weird online database retrieval stuff. – Elisabeth May 03 '16 at 20:22
  • @bns The second option of what? – Elisabeth May 03 '16 at 20:23
  • @Mangist What if I have the country AND the city? Should that work ? – Elisabeth May 03 '16 at 20:25
  • @Elisabeth http://www.geonames.org/export/ws-overview.html has an API you can call by city and will return the timezone. You can then use that to convert your UTC time to local. – Jon May 03 '16 at 20:27
  • @Mangist There exist no offline solution? – Elisabeth May 03 '16 at 20:30
  • @Elisabeth no. Take for instance USA. There are several timezones. There is no offline database that says New York City, USA is in Eastern Time (-5 GMT). You need to use a webservice, or better yet, have your users choose their timezone in your app, and store this with their user profile. – Jon May 03 '16 at 20:32
  • Yeah right US has 6 time zones! ok thanks, for the first version I can hardcode it. Later we need a conceptual change... we already use openstreetmap maybe I get timezone in the result ;-) – Elisabeth May 03 '16 at 20:38
  • @bns it seems the weird online database stuff is the solution :P or a geo coding service... – Elisabeth May 03 '16 at 20:40
  • @Elisabeth, in case you are in the local machine then you can use var dateTimeLocal = dateTimeUtc.ToLocalTime()) if the current culture is set correctly. In case you are on the server, you can use the offline solution I send you in the link. (if you read the answer you can see its actually a couple of xls files, not an online service). This may be not accurate, but at least you don't depend on a service. – bns May 03 '16 at 20:48
  • @Mangist - the `CurrentCulture` only affects *formatting*, not time zone. – Matt Johnson-Pint May 03 '16 at 21:10
  • @Elisabeth - See the updated duplicate link (I changed it, because the other was not very helpful). An online solution is not necessary, but *updates* to offline data are. – Matt Johnson-Pint May 03 '16 at 21:12
  • The nodatime is cool thing to get a timezone for a country which works for some countries at least... – Elisabeth May 04 '16 at 07:54

0 Answers0