Date and Time - Lakelands Computing

Title
Go to content
Date and Time
Being able to get the date or time is a common thing for a program. Pythons datetime module gives a way to do this:

  • import datetime
  • x = datetime.datetime.now()
  • print (x)

That would give this : 2020-07-08 15:46:55.647734

This date contains year, month, day, hour, minute, second, and microsecond in that order.

Knowing this you can choose the sections you want eg:

  • print (x.year)  would give the year - 2020
  • print (x.month) gives the month - 07
  • print (x.day) gives the day - 08
  • print (x.hour) gives the hour - 15
  • print (x.minute) gives the minute - 46
  • print(x.strftime("%A")) - gives the day of the week, so for this example Wednesday

There is a working version of this in the trinket. There is also a method for entering dates, and calculating the gap between two dates.
All Text copyright Lakelands Academy & Mr T Purslow 2020.
All images copyright free / creative commons unless otherwise stated.
You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
All Text copyright Lakelands Academy & Mr T Purslow 2020.  All images copyright free / creative commons unless otherwise stated. You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
All Text copyright Lakelands Academy & Mr T Purslow 2020.  All images copyright free / creative commons unless otherwise stated. You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
Back to content