How to obtain datetime.date and datetime.time from time.ctime() in python?

Viewed 63

I'm trying to access time.ctime() in order to make some simple boolean operations. I found this procedure:

datetime.datetime.strptime(time.ctime(), "%a %b %d %H:%M:%S %Y")

Which gives me this:

datetime.datetime(2017, 10, 5, 17, 7, 51)

How can I access time.ctime() date and time separate so I could get:

datetime.date(2017, 10, 5)

and

datetime.time(17, 7)
1 Answers

You can use the date() and time() methods to extract them:

dt = datetime.datetime(2017, 10, 5, 17, 7, 51)
d = dt.date()
t = dt.time()
Related