views:

86

answers:

3

Hi,

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?

+4  A: 
string s = DateTime.Now.ToString("HH:mm:ss");
Darin Dimitrov
Hi,I tried that one too, it adds the 'am/pm' stuff behind it.How can i prevent that? Shouldn't cultureinfo be added?
Yustme
@Yustme: Don't be confused between `DateTime.Now.TimeOfDay` and `DateTime.Now`
abatishchev
+1  A: 

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

Viktor Jevdokimov
Here is the link: http://msdn.microsoft.com/en-us/library/system.datetime_properties.aspx
abatishchev
+1  A: 

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");
theburningmonk
Thanks thebunringmonk! That did the trick. Can't believe how long i've been looking for a solution to fix this!
Yustme
@Yustme - 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.
theburningmonk