?
Solved

C# DateTime Formatting based on Locale and Timezone

Posted on 2010-04-06
3
Medium Priority
?
2,210 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:trailblazzyr55
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 29917999
0
 
LVL 20

Author Comment

by:trailblazzyr55
ID: 29918674
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
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 2000 total points
ID: 29919367
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question