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?
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.
You'll have to do the check yourself:
dateTime.ToString(dateTime.Minute == 0 ? "Htt" : "H:mmtt");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With