Programming, Python, Tutorials

Python DateTime Module

Unlike many other programming languages, Python does not have a default date object. Instead, it has a separate module for manipulating date and time. This module is called datetime.

The DateTime module has several different functions that can be used to work with date and time. These functions generally return date, time, or both in different formats.

Table of Content:

Types of Date and time objects

In python data and time objects can be categorized as “aware” or “naive”.

Aware Datetime object

If the date-time object includes information about the time zone, daylight saving, or geographical time adjustment it is called “aware” DateTime as it is not open to interpratation and is aligned with the geographical timezones. It has an additional tzinfo attribute

Naive Datetime objects

These type of datetime objects are open to interpretation as they do not include time zone information. Naive datetime is specific to the program and are mear number which lack context and standard time units.

Datetime module Constants

Datetime module exports 2 constans

  • datetime.MINYEAR ~ smallest year allowed by datetime module
  • datetime.MAXYEAR ~ largest year allowed by datetime module

Using datetime in python

Like any other module in python you can import datetime module by –

To import only specific function of datetime module use-

The datetime module has many classes such as “date”,”time”, “datetime”, “timezone”, “tzinfo” etc.

You can see the list of available classes using the dir function.


Get current date and time

use to get current local date and time.

Why datetime.datetime ??

the first datetime refers to the module and the later datetime refers to the “datetime” class of the “datetime module”

Current Date and time in python

Get Current date

To get the current date, we use the today() function of the datetime module defined in the date class.

Current date in python

Get Year, Month, Day

The today() function of the date class can also be used to get the year, month, or day separately.

Get year, month, day in python

Timestamp(Unix Epoch) to Human readable date

We can also convert a timestamp into a date using the timestamp() provided by the date class.

Creating a time object

The time class of the datetime module can be used to represent/ create a time object.

The arguments are in this order – hour, minutes, seconds, and milliseconds. (The fourth argument is optional)

Creating a Date Object

The date class can be used to create date objects.

Create time object in python

Formatting date and time

Date and time can be written in different formats such as MM-DD-YYYY or DD-MM-YYYY etc. or HH:MM:SS or MM:SS:HH

To format a date returned by the now() function of datetime class, we can use the strftime() function.

Format date time in python

To Learn more in depth about datetime module read date time documentation.

Comment below if you want me to cover time zone datetime.

Thank you for reading, Happy Learning, drop your suggestion in the comments.

Feel free to follow us on Youtube , Linked In , Instagram

0 0 vote
Article Rating