Python 5c - Time & Date
ctime()
The easiest way to output the current time and date is to use the ctime() command.
Import the ctime command from the time library before you use and print it:
from time import ctime
​
print("Current time:" , ctime())
=
Current time: Wed Sep 13 16:07:20 2023
This will print the time and date, but it looks rather unprofessional, and the exact format depends on the type of system that you are currently running so it may vary for different users.
Date / Time Task 1 (Dentist Surgery)
Print a greeting for a dentist surgery with the current date and time.
Example solution:
Welcome to Greenvale Dentist Surgery, it is currently: Wed Sep 13 16:16:24 2023
strftime()
A better alternative to the ctime() command is to use strftime() which stands for string from time as you can select specific parts of the date and time to display.
​
This command requires a directive to be written with a percentage symbol as a string in the brackets.
​
For example, the current hour (%H), minute (%M) and second (%S) can be printed between colons to show the time.
from time import strftime
​
print("The current time is" , strftime("%H:%M:%S"))
=
The current time is 13:18:57
There are many different directives that you can use to display exactly what you are looking for, such as:
from time import strftime
​
day = strftime("%A")
print("The current day is" , day)
​
month = strftime("%B")
print("The current month is" , month)
​
year = strftime("%Y")
print("The current year is" , year)
=
The current day is Thursday
The current month is September
The current year is 2023
The following directives can be used with strftime(). Don't forget that directives must be typed within speech marks.
Date -
Weekday:
%a – Current day of the week abbreviated (e.g. Sun, Mon)
%A – Current day of the week in full (e.g. Sunday, Monday)
%w – Current day of the week in chronological order (0 is Sunday and 6 is Saturday)
%W – Current week number (e.g. 01, 26, 52)
​
Month:
%d – Current day of the month (e.g. 01, 11, 31)
%m – Current month as a number (e.g. 01, 06, 12)
%b – Current month abbreviated (e.g. Jan, Jun, Dec)
%B – Current month in full (e.g. January, December)
​
Year:
%y – Current year abbreviated (e.g. 16, 17)
%Y – Current year in full (e.g. 2016, 2017)
%j – Current day of the year (e.g. 001, 150, 365)
Time -
Hour:
%H – Current hour in 24-hour clock (e.g. 00, 12, 20)
%I – Current hour in 12-hour clock (e.g. 01, 08, 12)
%p – Whether it is currently AM or PM
​
Minute:
%M – Current minute (e.g. 00, 30, 59)
​
Second:
%S – Current second (e.g. 00, 30, 59)
​
More Directives -
%z – Current time difference from UTC (Co-ordinated Universal Time) (e.g. +0000, -0500, +1100)
%Z – Current time zone (e.g. GMT Standard Time, EST, CST)
​
Just looking for a quick date or time display and not bothered about customisation? Try these:
%c – Current date and time in full (e.g. Tue Feb 19 13:35:20 2016)
%x – Current date (e.g. 19/02/16)
%X – Current time (13:36:20)
Date / Time Task 2 (Calendar App)
Create a program that asks the user if they want to see the current date, the current time or 'other'. Use the strfftime directives above to show what the user asks for.
​
It's up to you which directives you use for the 'other' option, such as displaying the current day of the year (%j) or current week of the year (%W).
Example solutions:
Type TIME for the current time, DATE for the current date or OTHER: TIME
The current time is 13:46PM
Type TIME for the current time, DATE for the current date or OTHER: DATE
The date today is Thursday 14 September 2023
Type TIME for the current time, DATE for the current date or OTHER: OTHER
Did you know today is day number 257 of 2023?
Between Dates
You may want to work out the number of days between two dates. This can be done by importing the date command from the timedate library. Below is a simple example:
from datetime import date
​
date1 = date(2021,9,15)
date2 = date(2022,1,20)
​
difference = date2 - date1
print("There are" , difference.days , "days between" , date1 , "and" , date2)
Make sure the date is entered in the format of year, month, day.
The .days code removes the difference in hours and seconds to just display the number of days difference.
There are 127 days between 2021-09-15 and 2022-01-20
Today's Date
The program here uses strftime to check the current year, month and day and organise it into the date format.
​
This can then be used together with code similar to the program above to check the number of days between one specific date and the current date.
from datetime import date
from time import strftime
​
thisyear = int(strftime("%Y"))​
thismonth = int(strftime("%m"))
thisday = int(strftime("%d"))
todaysdate = date(thisyear,thismonth,thisday)
print("The date today is" , todaysdate)
The date today is 2023-09-14
Input a Date
The program here shows how to input a date into a format that can then be used by Python to work out the difference between two dates.
from datetime import date
​
year = int(input("Enter a year: ")​
month = int(input("Enter a month: ")
day = int(input("Enter a day: ")
chosendate = date(year,month,day)
​
print("The chosen date is" , chosendate)
Enter a year: 1964
Enter a month: 5
Enter a day: 13
The chosen date is 1964-05-13
Date / Time Task 3 (Days Alive)
Create a program that works out how long the user has been alive for.
Use the examples above to automatically make today's date and then allow the user to input their year, month and day of birth.
Get Python to work out the difference between today and their date of birth.
Example solutions:
Enter a year: 1998
Enter a month: 3
Enter a day: 29
You have been alive for 9300 days!
Enter a year: 2007
Enter a month: 12
Enter a day: 25
You have been alive for 5742 days!