Reputation: 5259
I need to be able to get the current date as string in different formats.
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var utcNow = DateTime.UtcNow;
var localTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, timeZone);
Console.WriteLine(localTime.ToShortDateString());
The example above prints the date as 22-01-2018
, which is the danish format. But I need it to print the date as 1/22/2018
(example only).
I know that I can format dates using ToString
, but I need to be able to format the date from a range of different timezones (the user picks the timezone).
Can I somehow tell ToShortDateString
to use another culture and if so, how do I get from TimeZoneInfo
to CultureInfo
?
Upvotes: 2
Views: 871
Reputation: 39102
You cannot directly convert TimeZoneInfo
to CultureInfo
unfortunately.
The problem is that in any given timezone there are many countries which fall into it and hence many different culture settings.
The awesome Noda Time by Jon Skeet includes country code -> timezone mappings so you could use that to know which countries are in your given time zone. But from there, you will have to choose the target country somehow.
Upvotes: 3