开发者

C# time format to use "a.m." and "p.m." instead of AM/PM?

开发者 https://www.devze.com 2023-01-30 15:30 出处:网络
I can\'t find information on how to use the CultureInfo classes (or whatever is needed) to display a formatted time in \"a.m.\" and \"p.m.\" (note the periods) instead of the default AM and PM.

I can't find information on how to use the CultureInfo classes (or whatever is needed) to display a formatted time in "a.m." and "p.m." (note the periods) instead of the default AM and PM.

First, what culture info surrounds the use of "a.m."/"p.m." instead of AM/PM? Is there a way to do this using format providers and not a hack such as string repl开发者_运维百科acements? Thanks.


Look at what's available in the DateTimeFormatInfo class exposed by the CultureInfo.DateTimeFormat property. Its AMDesignator and PMDesignator properties let you set the strings used for those values.

Take special note of the Remarks section of that link, in particular:

The application can replace standard patterns with custom patterns by setting the associated properties of a writable DateTimeFormatInfo object. To determine if a DateTimeFormatInfo object is writable, the application should use the IsReadOnly property.

If you pull CultureInfo and its child info objects from the current, invariant, or a specific culture, they tend to be read-only by default. Be prepared to use the Clone() method on those objects to retrieve writable copies that you can return and use to set format strings.

Once you have a DateTimeFormatInfo with the AM/PM properties set as desired, you can supply that as the IFormatProvider used by methods like String.Format() and Object.ToString().

0

精彩评论

暂无评论...
验证码 换一张
取 消