开发者

C# format time "On The Hour"

I need to format the time from a DateTime object as a string. The catch is, if the time is "on the hour" as in 12:00AM, 8:00PM, I need to trim the zeros and display 12AM or 8PM.

Is there an easy way to do this that开发者_如何学编程 I am missing?


You'll have to do the check yourself:

dateTime.ToString(dateTime.Minute == 0 ? "Htt" : "H:mmtt");


Unless I'm missing something, you can do this:

DateTime date = DateTime.Now;
if (date.Minute == 0) {
   return date.ToString("Htt");
} else {
   return date.ToString("H:mmtt");
}

Obviously with the extra formatting wrapped around this. But that's the core of it.


Try:

myDateTime.ToString("htt");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜