0

I have two versions, both which don't work for a reason I can't tell.

First Version

from datetime import datetime, date

d = datetime.date(2011, 01, 01)
print(d)

Which gives

  File "timeSince.py", line 3
    d = datetime.date(2011, 01, 01)
                             ^
SyntaxError: invalid token

Second Version

from datetime import datetime, date

d = datetime.date(2011, 1, 1)
print(d)

Which gives

Traceback (most recent call last):
  File "timeSince.py", line 3, in <module>
    d = datetime.date(2011, 1, 1)
TypeError: descriptor 'date' requires a 'datetime.datetime' object but received
a 'int'

Running Python 3.3

4 Answers 4

3

You imported date from datetime, so this will work:

>>> d = date(2011, 1, 1)
>>> d
datetime.date(2011, 1, 1)

no need to put the datetime infront

when you import a method from a module, you no longer use the name of the module to call that method because you imported that specific method!

note:

Your first instance is invalid syntax because you can't have a 0 in the beginning of an integer in python. You must use it this way: date(2011, 1, 1) and not this way: date(2011, 01, 01)

Sign up to request clarification or add additional context in comments.

2 Comments

Still gives the same error. File "timeSince.py", line 3 d = date(2011, 01, 01) ^ SyntaxError: invalid token
you can't use the integers starting with a 0 in python! it doesn't work! This works when you actually use correct python syntax!
2

Your import statement is wrong (or at least not helpful), so you end up using a date method on the datetime class (which converts datetimes to dates) rather than the date class (which creates dates).

>>> from datetime import datetime, date
>>> datetime.date
<method 'date' of 'datetime.datetime' objects>
>>> datetime.date.__doc__
'Return date object with same year, month and day.'

Do this instead:

>>> import datetime
>>> datetime.date(2011,1,1)
datetime.date(2011, 1, 1)
>>> datetime.date.__doc__
'date(year, month, day) --> date object'

Comments

2

You are using integer literals with leading 0 digits, which are not allowed in Python 3.

In Python 2, these were interpreted as octal values:

>>> 017
15

but the syntax is ambiguous and confusing to those not coming from a C background, and only the 0o123 syntax with leading 0o is now supported. The old syntax was made a syntax error to prevent people that do know the C octal syntax from creating subtle bugs.

Both versions try to use the datetime.date() method without an instance. You want to use just date() instead:

>>> from datetime import date, datetime
>>> date(2011, 1, 1)
datetime.date(2011, 1, 1)
>>> datetime
<type 'datetime.datetime'>
>>> datetime.date
<method 'date' of 'datetime.datetime' objects>

1 Comment

My second version in the post is not.
0

Two separate problems. You can't write integers with leading 0s (which is why your first version fails), and you've imported date from the datetime module already, so you shouldn't be trying to re-reference it (which is why your second version fails). Your code should look like this:

from datetime import date
d = date(2011, 1, 1)
print(d) # prints "2011-01-01"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.