How to get only time from datetime C #

Suppose I have the value 6/22/2009 10:00:00. How can I get only 10:00 in the morning from this date.

+120
c # datetime time
Jun 22 '09 at 12:09
source share
18 answers

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 

Useful link: DateTime.ToString () patterns

+173
Jun 22 '09 at 13:41
source share

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

+67
Jun 22 '09 at 12:11
source share

In C #, only the DateTime type exists and it consists of a date and time part. If you do not need a Date for DateTime, set the default value as follows:

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

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

+52
Feb 26 '14 at 16:53
source share

You might want to learn the DateTime.ToShortTimeString () method.

In addition, there are many other methods and properties of a DateTime object that can help you create a date or time in any way, like. Just take a look at the documentation.

+21
Jun 22 '09 at 12:13
source share

Try the following:

 TimeSpan TodayTime = DateTime.Now.TimeOfDay; 
+11
Oct. 14 '16 at 2:41
source share

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

+6
Apr 10 2018-12-12T00:
source share

You can use the following code:

 DateTime dt = new DateTime(2009, 6, 22, 10, 0, 0); //Date 6/22/2009 10:00:00 AM string time = dt.ToString("hh:mm tt"); //Output: 10:00 AM time = dt.ToString("HH:mm tt"); //Output: 10:00 AM dt = new DateTime(2009, 6, 22, 22, 0, 0); //Date 6/22/2009 10:00:00 PM time = dt.ToString("hh:mm tt"); //Output: 10:00 PM time = dt.ToString("HH:mm tt"); //Output: 22:00 PM 
+6
Dec 18 '14 at 5:47
source share

You can use this

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

This value is in real time with a millisecond value and is set only for a while.

+5
Nov 30 '17 at 1:31 on
source share

You can just write

 string time = dateTimeObect.ToString("HH:mm"); 
+4
Sep 01 '14 at 4:38 on
source share
 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 
+4
Jan 02 '19 at 11:35
source share

This works for me. I discovered this when I had to work with DateTime.Date to get only a fraction of the date.

 var wholeDate = DateTime.Parse("6/22/2009 10:00:00 AM"); var time = wholeDate - wholeDate.Date; 
+3
Apr 03 '13 at 11:19
source share

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

+3
Nov 25 '13 at 13:04 on
source share

if you use gridview then you can only show 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}"/> 
+2
Nov 21 '17 at 5:49
source share

If you want to compare the time, not the dates, you can simply have a standard comparison date or match the date you use, 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 more effective ways, but your dates are the same.

+1
Jun 22 '09 at 12:20
source share

You should also consider DateTime Kind.

 public static DateTime GetTime(this DateTime d) { return new DateTime(d.TimeOfDay.Ticks, d.Kind); } 
0
Nov 28 '18 at 18:14
source share

Use DateTime.TryParse () instead

Obviously, this is not a complete answer, but simply an addition to the other answers.

0
Dec 18 '18 at 16:54
source share

In my case, it works well

 MyDateTime.ToLongTimeString() 
0
Jul 02 '19 at 21:00
source share

Easily

 DateTime dt = DateTime.Now; string Timeonly = dt.ToLongTimeString(); 

Greetings

-one
Aug 08 '19 at 21:08
source share



All Articles