hernanavella
hernanavella

Reputation: 5552

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

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)

Upvotes: 4

Views: 125

Answers (1)

Mureinik
Mureinik

Reputation: 311393

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()

Upvotes: 2

Related Questions