Show the date with datetime

The datetime module is useful to get the date in our programs.
Let’s take a look at it.

The methods

Let’s use dir to see the methods in datetime.

from datetime import datetime

now = datetime.today()
for method in dir(now):
	if not method.startswith("__"):
		print(method)

Running this script we will get this output:

astimezone
combine
ctime
date
day
dst
fold
fromisoformat
fromordinal
fromtimestamp
hour
isocalendar
isoformat
isoweekday
max
microsecond
min
minute
month
now
replace
resolution
second
strftime
strptime
time
timestamp
timetuple
timetz
today
toordinal
tzinfo
tzname
utcfromtimestamp
utcnow
utcoffset
utctimetuple
weekday
year
>>>

Write the date in a certain way

from datetime import datetime

now = datetime.today()

date = f"{now.day}/{now.month}/{now.year}"

print(date)

The output:

24/8/2018
>>> 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.