codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

42 / 68 topics
36List Comprehensions37Python Iterators38Python Generators39Python Decorators40Python Modules, Packages & PIP41Python Main Function (__name__ == '__main__')42Python Dates and Time43Python Regular Expressions44Python JSON
Tutorials/Python Programming/Python Dates and Time
🐍Python Programming

Python Dates and Time

Updated 2026-05-15
30 min read

Python Dates and Time

Working with dates and times is a common task in programming. Whether you're logging events, scheduling tasks, or analyzing data, understanding how to manipulate dates and times in Python is essential. In this tutorial, we'll explore the datetime module, which provides classes for manipulating dates and times in both simple and complex ways.

Introduction

The datetime module in Python provides several classes to handle date and time operations:

  • date: Represents a date (year, month, day).
  • time: Represents a time of day (hour, minute, second, microsecond).
  • datetime: Combines both date and time.
  • timedelta: Represents the difference between two dates or times.

We'll also cover how to format and parse dates using strftime() and strptime(), respectively. Additionally, we'll learn how to get the current time and use time.sleep() to pause execution for a specified duration.

Core Content

1. The date Class

The date class represents a date (year, month, day). You can create a date object using the date() method:

date_example.py
1from datetime import date
2
3# Create a date object
4d = date(2023, 10, 5)
5print(d) # Output: 2023-10-05
Output
2023-10-05

2. The time Class

The time class represents a time of day (hour, minute, second, microsecond). You can create a time object using the time() method:

time_example.py
1from datetime import time
2
3# Create a time object
4t = time(14, 30, 45)
5print(t) # Output: 14:30:45
Output
14:30:45

3. The datetime Class

The datetime class combines both date and time. You can create a datetime object using the datetime() method:

datetime_example.py
1from datetime import datetime
2
3# Create a datetime object
4dt = datetime(2023, 10, 5, 14, 30, 45)
5print(dt) # Output: 2023-10-05 14:30:45
Output
2023-10-05 14:30:45

4. The timedelta Class

The timedelta class represents the difference between two dates or times. You can create a timedelta object using the timedelta() method:

timedelta_example.py
1from datetime import timedelta
2
3# Create a timedelta object
4td = timedelta(days=5, hours=3)
5print(td) # Output: 5 days, 3:00:00
Output
5 days, 3:00:00

5. Formatting Dates and Times with strftime()

The strftime() method allows you to format date and time objects into strings:

strftime_example.py
1from datetime import datetime
2
3# Create a datetime object
4dt = datetime(2023, 10, 5, 14, 30, 45)
5
6# Format the datetime object
7formatted_date = dt.strftime("%Y-%m-%d %H:%M:%S")
8print(formatted_date) # Output: 2023-10-05 14:30:45
Output
2023-10-05 14:30:45

6. Parsing Dates and Times with strptime()

The strptime() method allows you to parse strings into date and time objects:

strptime_example.py
1from datetime import datetime
2
3# Parse a string into a datetime object
4date_string = "2023-10-05 14:30:45"
5dt = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
6print(dt) # Output: 2023-10-05 14:30:45
Output
2023-10-05 14:30:45

7. Getting the Current Time

You can get the current date and time using the now() method of the datetime class:

current_time_example.py
1from datetime import datetime
2
3# Get the current datetime
4now = datetime.now()
5print(now) # Output: e.g., 2023-10-05 14:30:45.123456
Output
e.g., 2023-10-05 14:30:45.123456

8. Using time.sleep()

The time.sleep() function pauses the execution of a program for a specified number of seconds:

sleep_example.py
1import time
2
3print("Starting...")
4time.sleep(2) # Pause for 2 seconds
5print("2 seconds have passed.")
Output
Starting...
2 seconds have passed.

Practical Example

Let's create a simple program that logs the current date and time every second for 10 seconds:

practical_example.py
1import time
2from datetime import datetime
3
4print("Logging start...")
5for _ in range(10):
6 now = datetime.now()
7 print(f"{now} - Logging")
8 time.sleep(1)
9print("Logging end.")
Output
Logging start...
2023-10-05 14:30:45.123456 - Logging
2023-10-05 14:30:46.123456 - Logging
2023-10-05 14:30:47.123456 - Logging
2023-10-05 14:30:48.123456 - Logging
2023-10-05 14:30:49.123456 - Logging
2023-10-05 14:30:50.123456 - Logging
2023-10-05 14:30:51.123456 - Logging
2023-10-05 14:30:52.123456 - Logging
2023-10-05 14:30:53.123456 - Logging
2023-10-05 14:30:54.123456 - Logging
Logging end.

Summary

ConceptDescription
dateRepresents a date (year, month, day).
timeRepresents a time of day (hour, minute, second, microsecond).
datetimeCombines both date and time.
timedeltaRepresents the difference between two dates or times.
strftime()Formats date and time objects into strings.
strptime()Parses strings into date and time objects.
now()Gets the current date and time.
time.sleep()Pauses the execution of a program for a specified number of seconds.

What's Next?

In the next tutorial, we'll explore Python Regular Expressions, which are powerful tools for working with text data. Regular expressions allow you to search, match, and manipulate strings in complex ways. Stay tuned!


PreviousPython Main Function (__name__ == '__main__')Next Python Regular Expressions

Recommended Gear

Python Main Function (__name__ == '__main__')Python Regular Expressions