Link to home
Start Free TrialLog in
Avatar of trailblazzyr55
trailblazzyr55

asked on

C# DateTime Formatting based on Locale and Timezone

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

Germany
--------------------
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.
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Avatar of trailblazzyr55
trailblazzyr55

ASKER

Thanks for the quick reply, I noticed that link when searching for answers before asking a question here, but I didn't see how they were accounting for the timezone. Is timezone being factored in there?
ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial