179

Suppose I have the value 6/22/2009 10:00:00 AM. How do I get only 10:00 Am from this date time.

Tyler Durden
  • 11,156
  • 9
  • 64
  • 126

14 Answers14

247

You have many options for this:

DateTime dt = DateTime.Parse("6/22/2009 07:00:00 AM");

dt.ToString("HH:mm"); // 07:00 // 24 hour clock // hour is always 2 digits
dt.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
dt.ToString("H:mm"); // 7:00 // 24 hour clock
dt.ToString("h:mm tt"); // 7:00 AM // 12 hour clock

Helpful Link: DateTime.ToString() Patterns

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Theofanis Pantelides
  • 4,724
  • 7
  • 29
  • 49
  • 24
    @MetroSmurf - the OP is, of course, not clear what he wants to do. One of my pet hates is to go to strings to early. I had a colleague who converted everything to string - even doubles etc. It's an accident waiting to happen. I would say don't convert objects to strings until you want to display the object to a human. Until then stick with the objects. – paul Dec 18 '14 at 06:26
  • 1
    For quick reference, `ss` is seconds with a leading zero and `s` is seconds without – Beevee Jul 26 '18 at 12:28
  • best answer so far – Tk1993 Jun 07 '19 at 22:15
90

From a DateTime, you can use .TimeOfDay - but that gives you a TimeSpan representing the time into the day (10 hours).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
83

There is only DateTime type in C# and it consist of both the date and time portion. If you don't care about the Date portion of DateTime, set it to default value like this:

DateTime myTime = default(DateTime).Add(myDateTime.TimeOfDay)

This way you can be consistent across all versions of .NET, even if Microsoft decides to change the base date to something else than 1/1/0001.

panpawel
  • 1,782
  • 1
  • 16
  • 17
23

You might want to look into the DateTime.ToShortTimeString() method.

Also, there many other methods and properties on the DateTime object that can help you in formating the date or time in any way you like. Just take a look at the documentation.

heavyd
  • 17,303
  • 5
  • 56
  • 74
  • +1 This is the cleanest solution, if you are running in an international environment with requirement of localization of the time output, since it returns the time in whatever format the current system has been set to. – aggaton Dec 05 '16 at 19:56
14

Try this:

TimeSpan TodayTime = DateTime.Now.TimeOfDay;
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
12
DateTime now = DateTime.Now;

now.ToLongDateString();    // Wednesday, January 2, 2019
now.ToLongTimeString();    // 2:33:59 PM
now.ToShortDateString();   // 1/2/2019
now.ToShortTimeString();   // 2:16 PM
now.ToString();            // 1/2/2019 2:33:59 PM
8

There are different ways to do so. You can use DateTime.Now.ToLongTimeString() which returns only the time in string format.

Leigh
  • 28,765
  • 10
  • 55
  • 103
  • You probably meant `DateTime.ToLongTimeString()` rather than `DateTime.Now.ToLongTimeString()`. Given the age of this thread, best not to resurrect it unless the response contributes a significant improvement over previous answers. – Leigh Apr 10 '12 at 18:15
6

You can simply write

string time = dateTimeObect.ToString("HH:mm");
shA.t
  • 16,580
  • 5
  • 54
  • 111
Kishan Gupta
  • 586
  • 1
  • 5
  • 18
  • 5
    this adds absolutely nothing to what has already been submitted over 4 years ago! – paul Dec 18 '14 at 06:19
6

You can use this

lblTime.Text = DateTime.Now.TimeOfDay.ToString(); 

It is realtime with milliseconds value and it sets to time only.

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
chrysanly
  • 61
  • 1
  • 1
  • TimeOfDay is a good way but it brings miliseconds part too you need to format your time with standard type. – sajadre Jun 02 '19 at 11:40
5

This works for me. I discovered it when I had to work with DateTime.Date to get only the date part.

var wholeDate = DateTime.Parse("6/22/2009 10:00:00 AM");
var time = wholeDate - wholeDate.Date;
ProfK
  • 49,207
  • 121
  • 399
  • 775
4

You can use ToString("T") for long time or ToString("t") for short time.

shA.t
  • 16,580
  • 5
  • 54
  • 111
matsmats
  • 502
  • 3
  • 12
2

If you're looking to compare times, and not the dates, you could just have a standard comparison date, or match to the date you're using, as in...

DateTime time = DateTime.Parse("6/22/2009 10:00AM");
DateTime compare = DateTime.Parse(time.ToShortDateString() + " 2:00PM");
bool greater = (time > compare);

There may be better ways to to this, but keeps your dates matching.

hugoware
  • 35,731
  • 24
  • 60
  • 70
2

if you are using gridview then you can show only the time with DataFormatString="{0:t}" example:

    By bind the value:-
<asp:Label ID="lblreg" runat="server" Text='<%#Eval("Registration_Time ", "{0:t}") %>'></asp:Label>

By bound filed:-
<asp:BoundField DataField=" Registration_Time" HeaderText="Brithday" SortExpression=" Registration Time " DataFormatString="{0:t}"/>
Bipul Roy
  • 163
  • 1
  • 14
2

You need to account for DateTime Kind too.

public static DateTime GetTime(this DateTime d)
{
    return new DateTime(d.TimeOfDay.Ticks, d.Kind);
}
Yepeekai
  • 2,545
  • 29
  • 22
  • No point for answering this question, it was asked 9 year ago. Also there are many good answers to this question already. – benshabatnoam Nov 28 '18 at 18:41
  • You are not returning the time. You should review your answer – Yepeekai Nov 28 '18 at 18:41
  • @Yepeekai There is no Time class in .NET. So a DateTime instance with no Date portion is one of the options you can do. You can also return a string but you can use that in DateTime operations. I guess you could consider a TimeSpan too. – user3720899 Oct 04 '19 at 13:23
  • 1
    imo the "new DateTime(d.TimeOfDay.Ticks)" is the cleanest answer so far – Fabianus May 28 '20 at 09:27