Time in C # format "In an hour"

Hi guys, I need to format the time from a DateTime object as a string ... Trick ... if the time is "per hour", like at 12:00 AM, 8:00 PM, I need to trim the zeros and display 12AM or 8PM. ..

Is there an easy way to do this that I am missing?

+5
source share
3 answers

You will need to check:

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

If I am 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 extra formatting wrapped around this. But this is the core.

+4
source

Try:

myDateTime.ToString("htt");
+1
source

All Articles