Datetime Activities
Datetime Activities
Contents
Date Time Activities ....................................................................................................................................................................................................... 1
Activity 1 Know your time! ........................................................................................................................................................................................ 2
Sample Expected Result ......................................................................................................................................................................................... 2
What to do ............................................................................................................................................................................................................. 2
Activity 2 Display Date and time ................................................................................................................................................................................ 3
Sample Expected Result ......................................................................................................................................................................................... 4
What to do ............................................................................................................................................................................................................. 4
Duration ......................................................................................................................................................................................................................... 5
Activity 3 Add 5 Seconds ............................................................................................................................................................................................ 5
Sample Expected Result ......................................................................................................................................................................................... 5
What to do ............................................................................................................................................................................................................. 5
Activity 4 Previous, Present and Future ..................................................................................................................................................................... 5
Sample Expected Result ......................................................................................................................................................................................... 6
What to do ............................................................................................................................................................................................................. 6
Activity 5 How many days until the National Day? .................................................................................................................................................... 7
What to do ............................................................................................................................................................................................................. 7
Activity 6 Order & Shipping........................................................................................................................................................................................ 8
Sample Expected Result ......................................................................................................................................................................................... 8
What to do ............................................................................................................................................................................................................. 8
Challenge 1 (Optional) - How fast is your computer? .............................................................................................................................................. 9
1
Python with AI Level 2
What to do ............................................................................................................................................................................................................. 9
Challenge 2 (Optional) Count Down Timer .............................................................................................................................................................. 10
Sample Expected Result ....................................................................................................................................................................................... 11
What to do ........................................................................................................................................................................................................... 11
What to do
1. Open Visual Studio Code, create a Python program file called datetime-ex1.py.
2. import the datetime module.
2
Python with AI Level 2
import datetime
print(now)
print(f"Current year is {now.year}")
print(f"Current month is {now.month}")
print(f"Current hour is {now.hour}")
print(f"Current minute is {now.minute}")
print(f"Weekday of the week is {now.weekday()}")
print(f"Day of the month is {now.day}")
4. Save all changes and run the program. You should get similar result above.
5. Now change the follow statement
now = datetime.datetime.now()
To
now = datetime.datetime(2020,9,12)
3
Python with AI Level 2
%H Hour 00-23 17
%I Hour 00-12 05
What to do
Open Visual Studio Code, create a Python program file called datetime-ex2.py. Add the following code:
import datetime
4
Python with AI Level 2
Duration
Activity 3 Add 5 Seconds
Write a Python program to add 5 seconds to the current time. Display both the current and the new time. Date should not be included.
What to do
1. Open Visual Studio Code, create a Python program file called add-seconds.py. Import the required module:
import datetime
now = datetime.datetime.now()
duration = datetime.timedelta(seconds=5)
5
Python with AI Level 2
Today is 2022-01-09
Yesterday is 2022-01-08
Tomorrow is 2022-01-10
Please note, the actual dates will be different than the sample.
What to do
1. Open Visual Studio Code, create a Python program file called today-yesterday-tomrrow.py. Import required module:
5. Display
print(f"Today is {today}")
print(f"Yesterday is {yesterday}")
print(f"Tomorrow is {tomorrow}")
6. Save all changes and run the program. Obverse the result.
6
Python with AI Level 2
to:
today = datetime.date(2020,12,31)
8. Save all changes and run the program. Obverse the result.
What to do
1. In Visual Studio Code, create a file called national-day.py. Add the following code:
# you can change the month and day to reflect your National day.
month = 7
day = 1
2. Then, you need to get the current day and the National day in this year.
now = datetime.datetime.now()
nationalday = datetime.datetime(now.year,month,day)
3. Next, check if the National Day has passed. If so, the next National Day is next year.
4. Now you need to get the duration between now and the National Day.
td = nationalday - now
print(f"There are {td.days} days until the National Day.")
7
Python with AI Level 2
• Regular 4 days
• Express 2 days
You will write a program to allow the user select the delivery options, and display the estimated delivery day, and return window.
What to do
1. In Visual Studio Code, create a file called shipping.py. Import the required module:
import datetime
8
Python with AI Level 2
-------------------------------
"""
3. Create a dictionary variable to store the delivery schedule.
deliver_schedule = {
"r": 4,
"e":2
}
4. Ask the user to select delivery method
delivery_method = input(display).lower()
5. Get the delivery days from the dictionary object.
delivery_days = deliver_schedule[delivery_method]
What to do
1. Open Visual Studio Code, create a Python file called how-fast.py.
2. You will use datetime module to measure time, so
9
Python with AI Level 2
import datetime
3. Then you define two variables. You will use the variable to control number of time you want to multiplications.
max_i = 1000
max_j = 5000
7. Then you can calculate the duration from start time to finish time.
duration = end – start
9. Write down the time, and compare it to your guess earlier. Are they close?
10
Python with AI Level 2
You will be using the time module. Then we will be using the sleep() function. To use this function you will first import the time module into our
code. time.sleep(n) function makes the code wait for ‘n’ seconds. So, for this countdown you will be using time.sleep(1) as we want the code to
wait for 1 second in between two successive prints.
What to do
1. You will import the time module.
import time
2. You will then ask the user to input the length of countdown in seconds.
t = input("Enter the time in seconds: ")
3. This value is sent as a parameter ‘t’ to the user- defined function countdown(). Any variable read using input function is a string. So, we type
convert this parameter to ‘int’ as it is of string type.
countdown(int(t))
5. Within the while loop body, you then use divmod() to calculate the number of minutes and seconds.
mins, secs = divmod(t, 60)
11
Python with AI Level 2
divmod will return t//60 to mins, and t%60 to secs. The first one if the quotient, the second one is reminder.
6. Then you will print the minutes and seconds on the screen using the variable timeformat.
timer = '{:02d}:{:02d}'.format(mins, secs)’
7. Using end = ‘\r’ we force the cursor to go back to the start of the screen (carriage return), so that the next line printed will overwrite the
previous one.
print(timer, end="\r")
9. You then decrement time so that the while loop can converge.
t -= 1
10. After the completion of the loop we will print “Blast Off!!!” to signify the end of the countdown.
print('Blast Off!!!')
12