anon
anon

Reputation:

Python string to datetime-date

I've got lots of dates that look like this: 16.8.18 (American: 8/16/18) of type string. Now, I need to check if a date is in the past or future but, datetime doesn't support the German format.

How can I accomplish this?

Upvotes: 2

Views: 51

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

from datetime import datetime

s = "16.8.18"
d = datetime.strptime(s, "%d.%m.%y")

if d > datetime.now():
    print('Date is in the future.')
else:
    print('Date is in the past.')

Prints (today is 20.7.2018):

Date is in the future.

The format used in strptime() is explained in the manual pages.

Upvotes: 2

Related Questions