2

I'm trying to get the current time, and compare it with a date taken from a string. This is the code I have:

import datetime

CurrentDate = str(datetime.datetime.now())
CurrentDate = datetime.strptime(CurrentDate, "%d/%m/%Y %H:%M")
print(CurrentDate)

ExpectedDate = "9/8/2015 4:00"
ExpectedDate = datetime.datetime.strptime(ExpectedDate, "%d/%m/%Y %H:%M")
print(ExpectedDate)

if CurrentDate > ExpectedDate:
    print("Date missed")
else:
    print("Date not missed")

But this is the error I get.

CurrentDate = datetime.strptime(CurrentDate, "%d/%m/%Y %H:%M") AttributeError: 'module' object has no attribute 'strptime'

pufAmuf
  • 7,415
  • 14
  • 62
  • 95
  • 1
    One question per post, don't compare dates as strings, and please try some basic debugging before you ask a question. Reading your error line should make it obvious that you are calling `strptime` wrong which should be corrected by a quick trip to the docs. – Two-Bit Alchemist Sep 09 '15 at 15:48
  • related: [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/q/26313520/4279) – jfs Sep 09 '15 at 17:31

4 Answers4

10

There's not much point in converting datetime.datetime.now() into a string, just so you can convert it right back to a datetime. Just leave it as-is.

import datetime

CurrentDate = datetime.datetime.now()
print(CurrentDate)

ExpectedDate = "9/8/2015 4:00"
ExpectedDate = datetime.datetime.strptime(ExpectedDate, "%d/%m/%Y %H:%M")
print(ExpectedDate)

if CurrentDate > ExpectedDate:
    print("Date missed")
else:
    print("Date not missed")

Result:

2015-09-09 12:25:00.983745
2015-08-09 04:00:00
Date missed
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • it may fail during DST transitions. You should use UTC time or timezone-aware datetime objects. Here're [possible solutions](http://stackoverflow.com/a/26313848/4279) – jfs Sep 09 '15 at 17:33
3

Inside datetime module, a class is named datetime aswell which you probably know since you did it right in the rest of the code.

Your third line should be:

CurrentDate = datetime.datetime.strptime(CurrentDate, "%d/%m/%Y %H:%M")

And insted of two datetimes there is only one. That line raises an error. Alternatively, you can just import the whole class:

from datetime import datetime

And there won't be any need to specify datetime two times which is a tad easier.

Edit: As Two-Bit Alchemist pointed out (which I honestly haven't noticed) is that you're comparing dates by a string which isn't going to work a good practice. Take a look at various snippets in this question about comparing dates in Python without converting them to strings.

Community
  • 1
  • 1
Mirza
  • 213
  • 2
  • 8
  • 1
    "you're comparing dates by a string which isn't going to work." Can you explain that a bit further? Why can't you compare two dates that you extracted from strings using `strptime`? Also, do you have any comment on the error `ValueError: time data '2015-09-09 11:47:34.298745' does not match format '%d/%m/%Y %H:%M'` that occurs when you modify the original code with your proposed change? – Kevin Sep 09 '15 at 15:59
  • My Python is a bit rusty. Apparently it does work, but in my opinion it's better practice to compare Date values, not dates-as-string ones. Google this, I'm wrong. I have edited my answer to reflect what you pointed out. And about your second question, you should try switching %d and %m. I think they are in different order :) – Mirza Sep 09 '15 at 16:09
  • Switching "d" and "m" still gives a ValueError. I'm pretty sure it's because the "seconds" portion of the string isn't being handled. – Kevin Sep 09 '15 at 16:26
  • Hmm, as pointed out [https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior](in the docs), %H delimiter is used for "zero-padded decimal numbers". I believe that is an explanation, since the date in ExpectedDate variable is "4:00". Try changing it to "04:00". Also if you're using 12-hour clock, use %I delimiter. – Mirza Sep 09 '15 at 16:33
  • But the error is occurring in the `CurrentDate` code, not the `ExpectedDate` code. – Kevin Sep 09 '15 at 16:35
  • @Kevin: there is no need to call `strptime()` on `CurrentDate`. It should be a datetime object already. – jfs Sep 09 '15 at 17:51
1

I have found out this excellent module that make date manipulation so simple:

import arrow

n = arrow.utcnow()
expected = arrow.get("9/8/2015 4:00", "D/M/YYYY H:m")

if n > expected:
    print("Date Missed.")
else:
    print("Date not missed.")
Ali SAID OMAR
  • 6,404
  • 8
  • 39
  • 56
  • note: `arrow` assumes that the input is UTC time here (that is good). But OP uses `datetime.now()` that implies that the input might be in local time (that might differ from UTC). – jfs Sep 09 '15 at 17:38
  • you're right, but it's seem that you can do : arrow.get(datetime.now()) :) – Ali SAID OMAR Sep 10 '15 at 07:58
  • it is also incorrect. It interprets the input naive datetime object as the time in UTC that is wrong (`datetime.now()` represents local time). Even if `Arrow` objects are used only for the comparison; it may fail for any local timezone with non-fixed utc offset (most of them). – jfs Sep 10 '15 at 10:39
0

One thing that string can't be compared to Datetime. And Just format both in

%Y-%m-%d

so that we can compare both... I have corrected your code and tried it. it's working.

and If you want to add time in it use this in both formating

%Y-%m-%d %H:%M:%S

import datetime

CurrentDate = datetime.datetime.now().strftime('%Y-%m-%d')
print(CurrentDate)

ExpectedDate = "9/8/2015"
ExpectedDate = datetime.datetime.strptime(ExpectedDate, "%d/%m/%Y").strftime('%Y-%m-%d')
print(ExpectedDate)

if CurrentDate > ExpectedDate:
    print("Date missed")
else:
    print("Date not missed")
Epic Ansh
  • 1
  • 1