问题
For an application we develop, we use the "G"
format everywhere in our application.
We want to change a little bit this format:
We need to display the first digit after the second.
Ex:
29.07.2014 08:54:36.1
We would like to be able to change this in the CurrentCulture
.
It's the only thing we want to change in the current culture, so if before, we were having the following format:
2014/07/29 08:54:36 AM
We then want to have
2014/07/29 08:54:36.1 AM
If before I was having
29.07.2014 08:54:36
We then want to have
29.07.2014 08:54:36.1
A nice to have would be, that if the DateTime
we receive, doesn't have a digit to be displayed here( =0 tenth of seconds), to don't have this displayed.
回答1:
You can set the CurrentCulture for the current thread and specify a deviating LongTimePattern:
CultureInfo culture = Thread.CurrentThread.CurrentCulture.Clone();
change the culture.DateTimeFormat.LongTimePattern = "your pattern";
Thread.CurrentThread.CurrentCulture = culture;
回答2:
I tested this and looks like is returning what you are looking for except for the "nice to have".
string now = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.f tt", CultureInfo.GetCultureInfo("en-US"));
回答3:
I'd create an extension method for DateTime, something like a ToPrettyPrintStringg() or whatever you wanna call it that returns the date in the format you wish, this way you can get it to display any date the way you want everywhere.
来源:https://stackoverflow.com/questions/25009870/modify-application-wide-the-display-of-datetime