codec
codec

Reputation: 8836

How to read the date from date time object?

I have a datetime object which looks like the following:

2020-04-08 11:23:19-05:00

How can I read just the date part from here? For example: 2020-04-08

Upvotes: 1

Views: 70

Answers (4)

Jaime
Jaime

Reputation: 323

myDateTime = datetime.now() #you get a datetime object
myDate = myDateTime.date() #you get a date object
print myDate               #print your date object
2016-05-11

Upvotes: 1

AquausDev
AquausDev

Reputation: 37

If you have that date set to a variable, such as time_and_date, you would use time_and_date[0, 9] = date to select the first 9 characters which are the date. the first '2' is the 0, and the '8' is the 9. You can also change this to select the time.

Upvotes: 0

Matthias Schreiber
Matthias Schreiber

Reputation: 2527

datetime_object.date().isoformat()

This creates a string that looks like you want it.

Upvotes: 1

bashrc
bashrc

Reputation: 4835

If you have a string of this format you can just split on whitespace

dateString.split(" ")[0]

where dateString is your input string

Upvotes: 1

Related Questions