Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Date or Time only from a DateTime Object

Tags:

c#

.net

datetime

I have a DateTime instance that has a Date and a Time. How do I extract only the date or only the time?

like image 746
Luke Avatar asked Nov 04 '10 13:11

Luke


People also ask

How can I get time from datetime?

How to Get the Current Time with the datetime Module. 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 do I get just the time in Python?

Apart from the datetime() module, the time module is another built-in way to get the current time in Python. As usual, you have to import the time module first, and then you can use the ctime() method to get the current date and time.


2 Answers

var day = value.Date; // a DateTime that will just be whole days var time = value.TimeOfDay; // a TimeSpan that is the duration into the day 
like image 170
Marc Gravell Avatar answered Sep 21 '22 21:09

Marc Gravell


You can also use DateTime.Now.ToString("yyyy-MM-dd") for the date, and DateTime.Now.ToString("hh:mm:ss") for the time.

like image 37
Joshua Smith Avatar answered Sep 21 '22 21:09

Joshua Smith