Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting only hour/minute of datetime

Tags:

c#

Using C#, I have a datetime object, but all I want is the hour and the minutes from it in a datetime object.

So for example: if I have a DateTime object of July 1 2012 12:01:02 All I want is July 1 2012 12:01:00 in the datetime object (so, no seconds).

like image 824
DantheMan Avatar asked Jun 19 '12 18:06

DantheMan


People also ask

How do I extract hours and minutes from DateTime?

To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.

How can I get minutes in C#?

DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25); Now, calculate the difference between two dates. TimeSpan ts = date2 - date1; To calculate minutes.

How do I show hours and minutes in C#?

String hourMinute = DateTime. Now. ToString("HH:mm"); Now you will get the time in hour:minute format.


1 Answers

Try this:

var src = DateTime.Now; var hm = new DateTime(src.Year, src.Month, src.Day, src.Hour, src.Minute, 0); 
like image 106
Roman Starkov Avatar answered Sep 24 '22 19:09

Roman Starkov