PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python Exercises » Python Date and Time Exercise with Solutions

Python Date and Time Exercise with Solutions

Updated on: June 5, 2025 | 10 Comments

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

import datetime

# Print date and time
print(datetime.datetime.now())

# only time
print(datetime.datetime.now().time())Code language: Python (python)

Solution 2 using time.strftime()

from time import gmtime, strftime

print(strftime("%Y-%m-%d %H:%M:%S", gmtime()))Code language: Python (python)

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
from datetime import datetime

date_string = "Feb 25 2020  4:20PM"
datetime_object = datetime.strptime(date_string, '%b %d %Y %I:%M%p')
print(datetime_object)Code language: Python (python)

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
from datetime import datetime, timedelta

given_date = datetime(2020, 2, 25)
print("Given date")
print(given_date)

days_to_subtract = 7
res_date = given_date - timedelta(days=days_to_subtract)
print("New Date")
print(res_date)Code language: Python (python)

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
from datetime import datetime

given_date = datetime(2020, 2, 25)
print("Given date is")
print(given_date.strftime('%A %d %B %Y'))Code language: Python (python)

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:

from datetime import datetime

given_date = datetime(2020, 7, 26)

# to get weekday as integer
print(given_date.today().weekday())

# To get the english name of the weekday
print(given_date.strftime('%A'))Code language: Python (python)

Solution 2 using calendar module

import calendar
from datetime import datetime

given_date = datetime(2020, 7, 26)
weekday = calendar.day_name[given_date.weekday()]
print(weekday)Code language: Python (python)

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 this timedelta to your datetime object.
Show Solution
from datetime import datetime, timedelta

given_date = datetime(2020, 3, 22, 10, 00, 00)
print("Given date")
print(given_date)

days_to_add = 7
res_date = given_date + timedelta(days=days_to_add, hours=12)
print("New Date")
print(res_date)Code language: Python (python)

Exercise 7: Print Current Time in Milliseconds

Show Solution
import time

milliseconds = int(round(time.time() * 1000))
print(milliseconds)Code language: Python (python)

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
from datetime import datetime

given_date = datetime(2020, 2, 25)
string_date = given_date.strftime("%Y-%m-%d %H:%M:%S")
print(string_date)Code language: Python (python)

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’s relativedelta. We can add 4 months into the given date using a relativedelta.
  • The relativedelta is useful when we want to deal months with day 29, 30 31, It will properly adjust the days.
from datetime import datetime

from dateutil.relativedelta import relativedelta

# 2020-02-25
given_date = datetime(2020, 2, 25).date()

months_to_add = 4
new_date = given_date + relativedelta(months=+ months_to_add)
print(new_date)Code language: Python (python)

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.

Show Solution
from datetime import datetime

# 2020-02-25
date_1 = datetime(2020, 2, 25).date()
# 2020-09-17
date_2 = datetime(2020, 9, 17).date()

delta = None
if date_1 > date_2:
    print("date_1 is greater")
    delta = date_1 - date_2
else:
    print("date_2 is greater")
    delta = date_2 - date_1
print("Difference is", delta.days, "days")
Code language: Python (python)

Filed Under: Python, Python Exercises

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Exercises

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Loading comments... Please wait.

In: Python Python Exercises
TweetF  sharein  shareP  Pin

  Python Exercises

  • All Python Exercises
  • Basic Exercise for Beginners
  • Intermediate Python Exercises
  • Input and Output Exercise
  • Loop Exercise
  • Functions Exercise
  • String Exercise
  • Data Structure Exercise
  • List Exercise
  • Dictionary Exercise
  • Set Exercise
  • Tuple Exercise
  • Date and Time Exercise
  • OOP Exercise
  • File Handling Exercise
  • Python JSON Exercise
  • Random Data Generation Exercise
  • NumPy Exercise
  • Pandas Exercise
  • Matplotlib Exercise
  • Python Database Exercise

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com

Advertisement