Display current time in this format: HH:mm:ss
Asked Answered
A

3

12

I'm having some trouble displaying the time in this format: HH:mm:ss. No matter what i try, i never get 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?

Abscond answered 1/8, 2010 at 10:39 Comment(1)
TimeSpan has a wimpy ToString() method. Changed in .NET 4.0, not for the better. Just lose "TimeofDay'. Darin got it right of course.Paella
G
9

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");
Gati answered 1/8, 2010 at 11:27 Comment(2)
Thanks thebunringmonk! That did the trick. Can't believe how long i've been looking for a solution to fix this!Abscond
@Abscond - No probs, glad I could help ;-) Globalization is not that well covered as not many of us do it, I did come across some good MSDN article on it a while back but can't seem to find the link now, will post an update if I find it again.Gati
D
23
string s = DateTime.Now.ToString("HH:mm:ss");
Decarlo answered 1/8, 2010 at 10:46 Comment(2)
Hi, I tried that one too, it adds the 'am/pm' stuff behind it. How can i prevent that? Shouldn't cultureinfo be added?Abscond
@Yustme: Don't be confused between DateTime.Now.TimeOfDay and DateTime.NowFaugh
G
9

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");
Gati answered 1/8, 2010 at 11:27 Comment(2)
Thanks thebunringmonk! That did the trick. Can't believe how long i've been looking for a solution to fix this!Abscond
@Abscond - No probs, glad I could help ;-) Globalization is not that well covered as not many of us do it, I did come across some good MSDN article on it a while back but can't seem to find the link now, will post an update if I find it again.Gati
I
1

TimeOfDay is a TimeSpan, which has only one ToString() without parameters. Use Darin's solution or a sample from MSDN documentation for TimeSpan.ToString()

Impala answered 1/8, 2010 at 11:2 Comment(1)
Here is the link: msdn.microsoft.com/en-us/library/…Faugh

© 2022 - 2024 — McMap. All rights reserved.