Display current time in this format: HH:mm:ss
I'm having some trouble displaying the time in this format: HH:mm:ss. No matter what i try, i never g开发者_Python百科et it in that format.
I want the time in the culture of the Netherlands which is "nl-NL".
This was one of my (although i forgot to keep the count) 1000th try:
CultureInfo ci = new CultureInfo("nl-NL");
string s = DateTime.Now.TimeOfDay.ToString("HH:mm:ss", ci);
What am i doing wrong?
string s = DateTime.Now.ToString("HH:mm:ss");
You need to use the TimeZoneInfo class, here's how to show the current time in the Eastern Standard Time time zone in HH:mm:ss format:
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
string s = TimeZoneInfo.ConvertTime(DateTime.Now, timeZone).ToString("HH:mm:ss");
To find all the timezones available, you can use
TimeZoneInfo.GetSystemTimeZones();
Looking through the returned value from the above, the Id for the time zone you need (Amsterdam I assume) is called W. Europe Standard Time:
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
string s = TimeZoneInfo.ConvertTime(DateTime.Now, timeZone).ToString("HH:mm:ss");
TimeOfDay is a TimeSpan, which has only one ToString() without parameters. Use Darin's solution or a sample from MSDN documentation for TimeSpan.ToString()
精彩评论