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.
LVL 20
trailblazzyr55Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

trailblazzyr55Author Commented:
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?
0
Roshan DavisCommented:
converting to different zones, you can use TimeZoneInfo.ConvertTimeBySystemTimeZoneId
e.g.
DateTime currentTime = DateTime.Now;
Console.WriteLine("Current Times:");
Console.WriteLine();
Console.WriteLine("Los Angeles: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Pacific Standard Time"));
Console.WriteLine("Chicago: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Central Standard Time"));
Console.WriteLine("New York: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Eastern Standard Time"));
Console.WriteLine("London: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "GMT Standard Time"));
Console.WriteLine("Moscow: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Russian Standard Time"));
Console.WriteLine("New Delhi: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "India Standard Time"));
Console.WriteLine("Beijing: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "China Standard Time"));
Console.WriteLine("Tokyo: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Tokyo Standard Time"));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.