Date And Time Handling In Python

Date and Time Handling in Python

When working with real-world applications, Python developers often encounter issues related to date, time, or datetime. Perhaps it’s figuring out how much time has passed since a particular event occurred, or scheduling a task for future execution. Date and Time are fundamental for understanding advanced modules in Python. The language provides several libraries to cope with time-related issues effectively in a robust manner.


Date And Time Handling In Python
Date And Time Handling In Python

In this tutorial, we’ll explore various techniques to manipulate, use, and calculate data related to date, time, and datetime in Python.

Table of Contents

  1. Python Time Module
  2. Python Datetime Module
  3. Python Calendar Module

Python Time Module

Python’s time module comes with several time-related functions. It provides functionalities such as representing time in code, manipulating time, and formatting time or sleep.

Firstly import the time module into your code using the following command.

import time

Let’s dive into a few core time methods:

  • time.time(): This function returns the number of seconds passed since epoch (the point where time starts). For UNIX system, January 1, 1970, 00:00:00 (UTC) is considered as the epoch.
import time
print(time.time())
  • time.sleep(seconds): This method suspends (delays) execution of the current thread for the given number of seconds.
import time
print("This is printed immediately.")
time.sleep(2.4)
print("This is printed after 2.4 seconds.")

Python Datetime Module

The Python datetime module is an in-built module that is used to manipulate dates and times. It allows us to create, manipulate, and extract information from date and time objects. To use it, import datetime at the start of your code.

import datetime

Here are a few main functions under the datetime module:

  • datetime.datetime.now(): This function returns the current date and time.
import datetime
print(datetime.datetime.now())
  • datetime.datetime.day/ month/ year: These functions return the day, month and year of a date respectively.
import datetime
now = datetime.datetime.now()
print(now.day, now.month, now.year)
  • datetime.datetime.strptime(): This method creates a datetime object from a string.
import datetime
datestring = "2023-06-15 22:47:00"
dt = datetime.datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
print(dt)
print(dt.year, dt.month, dt.day)

Python Calendar module

The Calendar module allows you to output calendars and provides additional useful functions related to the calendar.

The month() method from calendar module returns a month’s calendar in a multi-line string. Here is an example of using it:

import calendar
print (calendar.month(2023, 1))

Another interesting method from Calendar module is weekday(). This method is used to get the day of the week. The week starts on Monday as 0 and ends on Sunday as 6.

import calendar

print (calendar.weekday(2023, 1, 1))

Through this tutorial, we’ve discussed different Python modules that can assist in understanding and exploiting date and time in Python. Through the time module, you can use time-related functions. The Datetime module lets you work optimally with dates and times in Python, and the Calendar module outputs calendars and adds useful functions. Having a solid understanding of how to deal with time and date in Python can elevate your Python game to a new level by allowing you to solve a broader scope of problems.

Keep practicing and stay tuned for more informative and engaging Python content!

Share this article:

Leave a Comment