9

I have the following code inside my asp.net mvc web application, where StartDate is of type DateTime?:-

@item.StartDate.Value.TimeOfDay

The above will display the current time including seconds and milliseconds such as, which i do not want :-

10:17:54.6023744

but is there a way to display only the hour:minutes with either AM or PM ?? Thanks

John John
  • 1
  • 72
  • 238
  • 501

6 Answers6

22

DateTime has an overload of ToString() which allows you to pass a custom format.

DateTime dt = DateTime.Now;
String strDate="";
strDate = dt.ToString("MM/dd/yyyy");   // 07/21/2007 
strDate = dt.ToString("dddd, dd MMMM yyyy");   //Saturday, 21 July 2007
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm"); // Saturday, 21 July 2007 14:58
strDate = dt.ToString("dddd, dd MMMM yyyy hh:mm tt"); // Saturday, 21 July 2007 03:00 PM
strDate = dt.ToString("dddd, dd MMMM yyyy H:mm"); // Saturday, 21 July 2007 5:01 
strDate = dt.ToString("dddd, dd MMMM yyyy h:mm tt"); // Saturday, 21 July 2007 3:03 PM
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); // Saturday, 21 July 2007 15:04:10
strDate = dt.ToString("MM/dd/yyyy HH:mm"); // 07/21/2007 15:05
strDate = dt.ToString("MM/dd/yyyy hh:mm tt"); // 07/21/2007 03:06 PM
strDate = dt.ToString("MM/dd/yyyy H:mm"); // 07/21/2007 15:07
strDate = dt.ToString("MM/dd/yyyy h:mm tt"); // 07/21/2007 3:07 PM
strDate = dt.ToString("MM/dd/yyyy HH:mm:ss"); // 07/21/2007 15:09:29
strDate = dt.ToString("MMMM dd"); // July 21
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"); // 2007-07-21T15:11:19.1250000+05:30    
strDate = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"); // Sat, 21 Jul 2007 15:12:16 GMT
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); // 2007-07-21T15:12:57
strDate = dt.ToString("HH:mm"); // 15:14
strDate = dt.ToString("hh:mm tt"); // 03:14 PM
strDate = dt.ToString("H:mm"); // 5:15
strDate = dt.ToString("h:mm tt"); // 3:16 PM
strDate = dt.ToString("HH:mm:ss"); // 15:16:29
strDate = dt.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'"); // 2007-07-21 15:17:20Z
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); // Saturday, 21 July 2007 15:17:58
strDate = dt.ToString("yyyy MMMM"); // 2007 July
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • can we add a default time from [this](http://stackoverflow.com/questions/21104633/how-to-add-date-picker-bootstrap-3-on-mvc-5-project-using-the-razor-engine) bootstrap date picker eg: `datepicker value + TimeofDay` ? – Shaiju T Mar 21 '15 at 15:03
5

This will do it for you:

@item.StartDate.Value.TimeOfDay.ToString("HH:mm tt");
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
2

Use the ToString(string) overload and specify the format:

StartDate.Value.TimeOfDay.ToString("hh:mm tt")

This would be the same as

StartDate.Value.ToString("hh:mm tt")
Michael Mairegger
  • 6,833
  • 28
  • 41
2

I think you are looking for this:

StartDate.Value.TimeOfDay.ToString("hh:mm tt")
gleng
  • 6,185
  • 4
  • 21
  • 35
1

If you're using .NET 4 or later, you can specify a format string when converting to a string:

@item.StartDate.Value.TimeOfDay.ToString("hh mm tt")

But why not just format the date/time appropriately without extracting TimeOfDay? This will work in earlier versions of .NET too:

@item.StartDate.Value.ToString("hh mm tt")
Joe
  • 122,218
  • 32
  • 205
  • 338
-1

Have you tried DateTime.Date?

So Ideally it would be

@item.StartDate.Value.Date

EDIT:

I didn't read the question correctly, take a look here DateTime.ToString(string)

mkamioner
  • 2,451
  • 1
  • 17
  • 14