2

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?

Trooper Z
  • 1,617
  • 14
  • 31
  • 1
    It does if you either set the locale or provide a format... – cs95 Jul 20 '18 at 15:41
  • Ok but how do I do that? –  Jul 20 '18 at 15:43
  • `from datetime import datetime as dt; date = dt.strptime(string, format=...)` – cs95 Jul 20 '18 at 15:44
  • Sorry, I don't understand it either. What does the string mean / do? –  Jul 20 '18 at 15:50
  • See here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior Please try to figure this out. – cs95 Jul 20 '18 at 15:51
  • I tried: `datetime.strptime(today, format='de_DE')` but then it says: `TypeError: strptime() takes no keyword arguments` –  Jul 20 '18 at 15:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176427/discussion-between-niels-dingsbums-and-coldspeed). –  Jul 20 '18 at 15:59
  • Possible duplicate of [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – Mr. T Jul 20 '18 at 18:10

1 Answers1

2
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.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91