C# DateTime Formatting based on Locale and Timezone
Posted on 2010-04-06
I'm pretty new to C# and I'm curious how I would format dates and times taking into account locales and timezones.
USA East Coast
Example Set: Locale = "en-US", timezone = "America/New_York"
Date Output: April 6, 2010
Time Output: 10:17:59 AM
Full Output: April 6, 2010 10:17:59 AM EDT
Example Set: Locale = "de,GE", timezone = "Europe/Berlin"
Date Output: 6. April 2010
Time Output: 16:17:59
Full Output: 6. April 2010 16:17 Uhr MESZ
In Java I would use java.text.DateFormat and run the following methods...
Date Output: DateFormat.getDateInstance(DateFormat.LONG, locale)
Time Output: DateFormat.getTimeInstance(DateFormat.MEDIUM, locale)
Full Output: DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, locale)
Where locale would be an object of java.util.Locale
Then set the timezone: DateFormat.setTimeZone(timeZone)
Where timezone would be the result of using java.util.TimeZone, calling getTimeZone(String timeZone) and passing it one of the timezones listed above in the example.
Then calling DateFormat.format(Now()) to produce the output above for Date, Time, and Full outputs.
So the output examples above are based on Java and I want to see what the equivalent would be in C# to produce the same results. I don't want to be tied to specifying a particular format other than LONG, MEDIUM, etc... since the format needs to be based on locale.