This “Date and Time” exercise aims to help Python developers learn and practice frequently occurring DateTime-related problems. Here, I am providing 10 date and time coding problems to solve to help you brush up your coding skills.
This Python DateTime exercise contains 10 coding questions, each with a provided solution. This coding exercise will help you to practice different dates and time programs and challenges.
It covers questions on the following topics:
- Working with dates and times in Python:
- Functions available in the Python
datetime
module - Convert and manipulate date and time in a specific format
- Date and time arithmetic
When you complete each question, you get more familiar with the DateTime operations. Let us know if you have any alternative solutions. It will help other developers.
Use Online Code Editor to solve exercise questions.
Table of contents
- Exercise 1: Print Current Date and Time
- Exercise 2: Convert String Into Datetime Object
- Exercise 3: Subtract a Week From a Given Date
- Exercise 4: Format DateTime
- Exercise 5: Find Day of Week
- Exercise 6: Add Week to Given Date
- Exercise 7: Print Current Time in Milliseconds
- Exercise 8: Convert Datetime into String
- Exercise 9: Calculate the date 4 months from the current date
- Exercise 10: Calculate Days Between Two Dates
Exercise 1: Print Current Date and Time
Refer: Get Current Date and Time in Python
Expected Output: (output may wary depending when you are running a code)
2025-06-05 08:27:00.498282
+ Hint
The datetime
module has a class named datetime
. This class has a static method now()
.
Show Solution
Use datetime
module
Solution 2 using time.strftime()
Exercise 2: Convert String Into Datetime Object
Write a code to convert the given date in string format into a Python DateTime
object.
Refer: Python String to DateTime
Given:
date_string = "Feb 25 2020 4:20PM"
Code language: Python (python)
Expected output:
2020-02-25 16:20:00
+ Hint
The datetime
class has a method strptime()
specifically designed to parse strings into datetime objects.
You’ll need to tell this method the exact format of your input string using format codes (e.g., %Y
for year, %m
for month, %d
for day, %H
for hour, %M
for minute, %S
for second).
Show Solution
Exercise 3: Subtract a Week From a Given Date
Write a code to subtract a week (7 days) from a given date.
Refer: TimeDelta in Python
Given:
given_date = datetime(2020, 2, 25)
Code language: Python (python)
+ Hint
Use timedelta class
Expected output:
2020-02-18
Show Solution
Exercise 4: Format DateTime
Write a code to print date in the following format.
Day_name Day_number Month_name Year
Refer: Python DateTime Format Using Strftime()
Given:
given_date = datetime(2020, 2, 25)
Code language: Python (python)
Expected output:
Tuesday 25 February 2020
Refer Date format codes for help
+ Hint
Use specific format codes in strftime()
method to achieve the desired output (e.g., %A
for full weekday name, %d
for day of the month, %B
for full month name, %Y
for year).
Show Solution
Exercise 5: Find Day of Week
Write a code to find the day of the week of a given date.
Given:
given_date = datetime(2020, 7, 26)
Code language: Python (python)
Expected output:
Sunday
+ Hint
- Use specific format codes in
strftime()
method to get name - Or use calendar module’s
day_name()
method.
Show Solution
Solution 1:
Solution 2 using calendar module
Exercise 6: Add Week to Given Date
Write a code to add a week (7 days) and 12 hours to a given date.
Given:
# 2020-03-22 10:00:00
given_date = datetime(2020, 3, 22, 10, 0, 0)
Code language: Python (python)
Expected output:
2020-03-29 22:00:00
+ Hint
- Use the
timedelta
class. - Create a
timedelta
object that combines days and hours. - Use the
+
operator to add thistimedelta
to yourdatetime
object.
Show Solution
Exercise 7: Print Current Time in Milliseconds
Show Solution
Exercise 8: Convert Datetime into String
Write a code to convert a given datetime object into a string.
Given:
given_date = datetime(2020, 2, 25)
Code language: Python (python)
Expected output:
"2020-02-25 00:00:00"
Show Solution
Exercise 9: Calculate the date 4 months from the current date
Given:
# 2020-02-25
given_date = datetime(2020, 2, 25).date()
Code language: Python (python)
Expected Output:
2020-06-25
+ Hint
For exact month arithmetic, the dateutil
library (specifically dateutil.relativedelta
) is the standard Python solution, as timedelta
only works with fixed durations (days, seconds, etc.).
Show Solution
Solution:
- We need to use the Python
dateutil
module’srelativedelta
. We can add 4 months into the given date using arelativedelta
. - The
relativedelta
is useful when we want to deal months with day 29, 30 31, It will properly adjust the days.
Exercise 10: Calculate Days Between Two Dates
Write a code to calculate the days between two dates.
Refer: Python Difference Between Two Dates in Days.
Given:
# 2020-02-25
date_1 = datetime(2020, 2, 25)
# 2020-09-17
date_2 = datetime(2020, 9, 17)
Code language: Python (python)
Expected output:
205 days
+ Hint
Subtract one datetime
object from another, the result is a timedelta
object. The timedelta
object has days
attribute that gives you the number of days.