仅使用日期时间格式获取小写日期时间的 AM/PM
我要获取包含 AM/PM 指示符的自定义日期时间格式,但我希望“AM”或“PM”为小写而不使其余字符小写。
是否可以使用单一格式而不使用正则表达式?
这是我现在得到的:
item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt")
现在的输出示例为2009 年 1 月 31 日星期六下午 1:34
I'm to get a custom DateTime format including the AM/PM designator, but I want the "AM" or "PM" to be lowercase without making the rest of of the characters lowercase.
Is this possible using a single format and without using a regex?
Here's what I've got right now:
item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt")
An example of the output right now would be Saturday, January 31, 2009 at 1:34PM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我个人会将其格式化为两部分:非 am/pm 部分和使用 ToLower 的 am/pm 部分:
另一个选项(我将立即研究)是获取当前的 DateTimeFormatInfo,创建一个副本,然后将 am/pm 指示符设置为小写版本。 然后使用该格式信息进行正常格式化。 显然,您想要缓存 DateTimeFormatInfo...
编辑:尽管我发表了评论,但无论如何我已经编写了缓存位。 它可能不会比上面的代码更快(因为它涉及锁和字典查找),但它确实使调用代码更简单:
这是一个完整的程序来演示:
I would personally format it in two parts: the non-am/pm part, and the am/pm part with ToLower:
Another option (which I'll investigate in a sec) is to grab the current DateTimeFormatInfo, create a copy, and set the am/pm designators to the lower case version. Then use that format info for the normal formatting. You'd want to cache the DateTimeFormatInfo, obviously...
EDIT: Despite my comment, I've written the caching bit anyway. It probably won't be faster than the code above (as it involves a lock and a dictionary lookup) but it does make the calling code simpler:
Here's a complete program to demonstrate:
您可以将格式字符串分成两部分,然后将 AM/PM 部分小写,如下所示:
但是,我认为更优雅的解决方案是使用
DateTimeFormatInfo
实例,您构建并替换AMDesignator
和PMDesignator
属性分别带有“am”和“pm”:您可以使用
DateTimeFormatInfo
实例自定义将DateTime
转换为string
的许多其他方面。You could split the format string into two parts, and then lowercase the AM/PM part, like so:
However, I think the more elegant solution is to use a
DateTimeFormatInfo
instance that you construct and replace theAMDesignator
andPMDesignator
properties with "am" and "pm" respectively:You can use the
DateTimeFormatInfo
instance to customize many other aspects of transforming aDateTime
to astring
.编辑:乔恩的示例要好得多,尽管我认为扩展方法仍然是可行的方法,因此您不必到处重复代码。 我已经删除了替换并替换了扩展方法中 Jon 的第一个示例。 我的应用程序通常是 Intranet 应用程序,我不必担心非美国文化。
添加一个扩展方法来为您执行此操作。
编辑:有关如何执行此操作的其他想法,请访问如何在 C# 中格式化日期时间,例如“2008 年 10 月 10 日 10:43am CST”。
EDIT: Jon's example is much better, though I think the extension method is still the way to go so you don't have to repeat the code everywhere. I've removed the replace and substituted Jon's first example in place in the extension method. My apps are typically intranet apps and I don't have to worry about non-US cultures.
Add an extension method to do this for you.
EDIT: Other ideas on how to do this at How to format a DateTime like "Oct. 10, 2008 10:43am CST" in C#.
上述方法的问题在于,使用格式字符串的主要原因是为了启用本地化,并且到目前为止给出的方法对于任何不希望包含最后的 am 或 pm 的国家或文化来说都是无效的。 因此,我所做的就是写出一个扩展方法,该方法可以理解附加格式序列“TT”,它表示小写的 am/pm。 下面的代码针对我的情况进行了调试,但可能还不完美:
The problem with the above approaches is that the main reason you use a format string is to enable localization, and the approaches given so far would break for any country or culture that does not wish to include a final am or pm. So what I've done is written out an extension method that understands an additional format sequence 'TT' which signifies a lowercase am/pm. The below code is debugged for my cases, but may not yet be perfect:
这应该是所有这些选项中性能最高的。 但太糟糕了,他们无法在 DateTime 格式中使用小写选项(tt 与 TT 相反?)。
This should be the most performant of all these options. But too bad they couldn't work in a lowercase option into the DateTime format (tt opposite TT?).