有人可以指导我使用正确的格式字符串来以下列格式显示时间吗?
Original: #7/28/2011 09:00:03 AM#
Required Format: 9am
//BUT if the minutes part contains a value I want it to look like this
Original: #7/28/2011 09:21:03 AM#
Required Format: 9:21am
我可以使用一种格式字符串来完成此操作吗?
PS:我已经尝试过 h:mmtt
但正如预期的那样并没有完全满足我的需求
最佳答案
格式字符串无济于事。您想要根据值有条件地格式化字符串。您必须自己检查分钟值并根据是否为 0 分钟应用适当的格式字符串。
DateTime dateValue = DateTime.Parse("7/28/2011 09:00:03 AM");
string formattedString;
if (dateValue.Minute == 0)
formattedString = dateValue.ToString(@"M/d/yyyy Htt");
else
formattedString = dateValue.ToString(@"M/d/yyyy H:mmtt");
关于c# - 日期时间格式字符串模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6853517/