How would I display the current time that could be either in 24 hour format or am/pm format depending on windows setting in C#?
+6
A:
This pulls the current thread's culture information for formatting:
// short time pattern, could be "6:30 AM" or "6:30"
DateTime.Now.ToString("t", CultureInfo.CurrentCulture)
// long time pattern, could be "6:30:00 AM" or "6:30:00"
DateTime.Now.ToString("T", CultureInfo.CurrentCulture)
And this pulls the current operating system's (Windows) installed culture information for formatting:
DateTime.Now.ToString("t", CultureInfo.InstalledUICulture)
DateTime.Now.ToString("T", CultureInfo.InstalledUICulture)
Edit to show just the time.
280Z28
2009-07-15 23:54:40
A:
Here is a way to do custom time in a very customizable way:
DateTime date = DateTime.Now;
string time = date.ToString("hh:mm:ss tt", CultureInfo.InvariantCulture);
The "tt
" formatter is what specifies AM or PM. You customize the time even further if you want. Here's a great MSDN article that lists the custom formatters and examples: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
mc2thaH
2009-07-16 04:56:02
You don't want it customizable in code. You want the code to use what the system has already set as the standard.
280Z28
2009-07-16 05:03:30