#! /usr/bin/python3.6
#
#
# dates are easily constructed and formatted
from datetime import date
now = date.today()
now
datetime.date(2003, 12, 2)
now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
#'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
# dates support calendar arithmetic
birthday = date(1964, 7, 31)
age = now - birthday
age.days
# File "bday.py", line 5
# from datetime import date
# ^
# IndentationError: unexpected indent
This is indented by a space. I needs to be against the left edge.
Cheers,
Cameron Simpson cs@cskk.id.au
thanks. I got it corrected.
I got it from here.
https://docs.python.org/3/tutorial/stdlib.html#dates-and-times
It looks mostly designed to use from the command line.
I am looking for commands to use in an actual .py script.
>>> # dates are easily constructed and formatted
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
>>> # dates support calendar arithmetic
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368
The tutorial used interactive examples, but all stdlib modules are intended for files also.
Click on the button in the top-right corner to get rid of the REPL (interactive mode) prompt and make the code easier to copy. The space is part of the prompt ">>>
", not parto of the Python code.
The first statement of a Python program cannot be indented. You start at indentation level 0.
1 Like