Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

String & Time Modules
Date/Time Module

Objective

Adjust records and set up shipping dates for supplies using Date & Time module functions.

The lower floor of the annex of the new construction area is a warehouse containing some of the recently delivered supplies that have been packed and ready to ship to areas around the new construction. They need to be labeled with their correct dates and times, and ensure that any labels that have already been applied have all relevant information. To do this we could take the time to manually write and format dates and time, but there’s a much quicker way to do this by using the date and time module.

The date and time module is imported by writing import datetime , this unlocks a series of powerful functions that allow you to read and set formatted times and dates which you can store in objects. You can also use: from datetime import timezone, timedelta in order to set time zones. The following functions are unlocked by importing the module:

  • datetime.datetime(): Allows you to set and store a time of your choosing, takes several arguments as follows: year, month, day, hour, minute, second, microsecond , timezone. You don’t have to enter all arguments, but rather the ones you wish to use.
  • datetime.datetime.now(): Returns your own current date and time as it would appear in a clock.
  • datetime.datetime.date() : Returns the datetime date only, no time included
  • datetime.datetime.time() : Returns the datetime time only, no date included
  • datetime.datetime.timestamp() : Returns the datetime’s timestamp as a UNIX code.
  • timezone(): Allows you to set a time zone for use with your dates and time arguments.
  • datetime.strftime(): Allows you to extract specific information of date and time and format it to your liking using specific codes. For our purposes we’ll be using the following codes:
    • "%a" : Weekday , shortened
    • "%d" : Day of the month
    • "%B" : Month name, written in full
    • "%I" : Hour , twelve hour cycle
    • "%M" : Minute
    • "%p" : Meridian, AM/PM
    • "%x" : Local date format
    • "%X" : Local time format
    • "%j" : Day of the year
    • "%U" : Week of the year
    • "%z" : UTC time zone

To start off walk to the light X mark and face the desk, use the read() function to check the packing dates for the shipments as well as start and end dates for the projects. Make note of the start and end dates provided in the document (month/day/year). The three (3) first data points written in the memo as well as the time zone are pre-written in the code editor for ease of use in later functions.

On this same space, create a variable named today and store the return value of datetime.datetime.now() . Use str() to convert the value into a string so it’s in a readable format, like this: today = str(datetime.datetime.now()). Use the display() function to chart down today’s date on the memo, with this the date for the shipping allocations is set and you can begin adding missing data points to the necessary areas.

Walk to the gold X mark next to the desk and here we’ll be calculating and charting down the number of days this project will take. There are two variables written in the editor named start and end that store datetime. Write down the start and end dates you previously read into the spaces provided. Create a variable named result and store the result of the of a subtraction between the end and start variables. Use the str() function to convert the value into a readable string format, like this: result = str(end-start) . Use the write() function to chart down the result variable, to mark down how long this will take.

From here we’re going to move on to the gold X mark next to the red carpet and will be moving down to the X marks next to the red carpet crates on the left hand column. Here we will be adding specific parts of the packing dates that are missing from the crates using datetime() . In the editor there will be a pre-written date allocated to the variable red_date , we’ll be extracting various bit’s of data and using it to label the crates.

For the red carpets:

  • On the gold X mark , face the crates and create a variable named red_days store in it the date without the time using date() from the red_date variable and, convert it using str(), like this: red_days = str(red_date.date()) . Use the write() function to label the crates using red_days .

  • On the light X mark , face the crates and create a variable named red_time store in it the time without the date using time() from the red_date variable and convert it using str(), like this: red_time = str(red_date.time()) . Use the write() function to label the crates using red_time .

  • On the dark X mark , face the crates and create a variable named red_timestamp store in it the timestamp calculated from the red_date variable, like this: red_timestamp = red_date.timestamp() . Use the write() function to label the crates using red_timestamp .

Follow this up with moving onto X marks in front of blue carpet crates. Like before there is a series of three (3) colored X marks, in this case referencing the pre-written blue_date variable to complete the labels. Go through the column and use the strftime() function to add missing elements to the shipping labels. Unlike the previous functions this one requires a specific code to extract and format elements in the target date. For example, if we want to get the day of the week for the blue_date variable you will use the code "%a" with the strftime() function like this: blue_date.strftime("%a") .

For the X marks next to the blue carpet crates, use the strftime() function with the specific code required. Look through the previously outlined code list depending on the requirements. For the blue carpets:

  • On the gold X mark , face the crates and create a variable named blue_weekday store in it the day of the week using strftime() from the blue_date variable like this: blue_day = blue_date.strftime("%a") . Create another variable named blue_day and store the day of the month using strftime() from the blue_date variable. Create another variable named blue_monthand store the month name, written in full, using strftime() from the blue_date variable. Insert the blue_weekday , blue_day and blue_month variables in the pre-written write() function.

  • On the light X mark , face the crates and create a variable named blue_hour store in it the hour , twelve hour cycle, using strftime() from the blue_date variable. Create another variable named blue_minute and store the minute using strftime() from the blue_date variable. Create another variable named blue_meridian and store the Meridian, AM/PM, using strftime() from the blue_date variable. Insert the blue_hour , blue_minute and blue_meridian variables in the pre-written write() function.

  • On the dark X mark , face the crates and create a variable named blue_local_date store in it the local date format, using strftime() from the blue_date variable. Create another variable named blue_local_time and store the local time format using strftime() from the blue_date variable. Insert the blue_local_date , blue_local_time and blue_meridian variables in the pre-written display() function.

Finally, we’re going to move on to the gold X mark next to the green carpet and will be moving down to the X marks next to the green carpet crates on the right hand column. Here we will continue adding specific parts of the packing dates that are missing from the crates using strftime() . In the editor there will be a pre-written date allocated to the variable green_date , we’ll be extracting various bit’s of data and using it to label the crates.

For the green carpets:

  • On the gold X mark , face the crates and create a variable named green_day store in it the day of the year using strftime() from the green_date variable. Use the write() function to label the crates using green_day.

  • On the light X mark , face the crates and create a variable named green_week store in it the week of the year using strftime() from the green_date variable. Use the write() function to label the crates using green_week.

  • On the dark X mark , face the crates and create a variable named green_timezone store in it the UTC time zone using strftime() from the green_date variable. Use the write() function to label the crates using green_timezone.

Once all labels on X marks next to colored carpets have been charted down, the level will be complete.

Code book