Reputation: 8836
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
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
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
Reputation: 2527
datetime_object.date().isoformat()
This creates a string that looks like you want it.
Upvotes: 1
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