What day is today?
It's lunch time and you are talking with some of your colleagues. They comment that they feel that every morning someone should send them a reminder of what day it is so they can check in the calendar what their assignments are for that day.
You want to help out and decide to write a small script that takes the date and time of the day so that every morning, a message is sent to your colleagues. You can use the module datetime
along with named placeholders to achieve your goal.
The date should be expressed as Month day, year
, e.g. April 16, 2019
and the time as hh:mm
, e.g. 16:30
.
You write down some specifiers to help you: %d
(day), %B
(monthname), %m
(monthnumber), %Y
(year), %H
(hour) and %M
(minutes)
You can use the IPython Shell to explore the module datetime
.
This exercise is part of the course
Regular Expressions in Python
Exercise instructions
- Import the function
datetime
from the moduledatetime
. - Obtain the date of today and assign it to the variable
get_date
. - Complete the string
message
by adding to the placeholders namedtoday
and the format specifiers to format the date asmonth_name day, year
and time ashour:minutes
. - Print the message using the
.format()
method and the variableget_date
to replace the named placeholder.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import datetime
____
# Assign date to get_date
get_date = ____
# Add named placeholders with format specifiers
message = "Good morning. Today is {____:____ ____, ____}. It's {today:___:____} ... time to work!"
# Use the format method replacing the placeholder with get_date
print(____.____(____))