datetime module

Python does not have a direct implementation to work with dates and time but we can use a built-in module called “datetime” to work with dates and time as objects. Consider an example:

import datetime
current_date_time = datetime.datetime.now()

print(current_date_time)

When we run this, we get:

The output is displayed in the format of Year-Month-Day Hour:Minute:Second.Microsecond. Each of these can be accessed individually as well. Consider the example:

import datetime

current_date_time = datetime.datetime.now()

print(current_date_time)

# Get current Year
print("\nYear:", current_date_time.year)

# Get current Day
print("Day:", current_date_time.day)

# Get current Minute
print("Minute:", current_date_time.minute)

# Get current Microsecond
print("Microsecond:", current_date_time.microsecond)

We get the output as:

Datetime is a class and when can create an instance of the class to work with its various features and it assumes the Georgian calendar. We can also create our custom date object using ‘datetime’. Its syntax is:

    • datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
      • year – Specify a custom year
      • month – Specify a custom month
      • day – Specify a custom day
      • hour – Specify a custom hour, the default value is 0
      • minute – Specify a custom minute, the default value is 0
      • second – Specify a custom second, the default value is 0
      • microsecond – Specify a custom microsecond, the default value is 0
  • tzinfo – Specify a time zone, the default value is None

Let’s create a custom datetime object.

import datetime

custom_date_time_1 = datetime.datetime(1969, 5, 27)
custom_date_time_2 = datetime.datetime(1969, 5, 27, 12, 41, 5)

print("Without time", custom_date_time_1)
print("With time", custom_date_time_2)

We see the output as:

Python also offers two other functions called strftime() and strptime() which convert these datetime objects into human-readable strings. The parameter for these functions is the codes to which we want to convert them into. 

FUNCTION 1: strftime()

Consider an example:

import datetime

current_date_time_object = datetime.datetime.now()

# Get minute
print("Minute:",current_date_time_object.strftime("%M"))

# Get microsecond
print("Microsecond:",current_date_time_object.strftime("%f"))

# Get local date
print("Local Date:",current_date_time_object.strftime("%x"))

We get the output as:

Here are some of the other options we can use in our function.

Code Description Example
%a Weekday (short) Wed
%A Weekday (full) Wednesday
%b Month (short) Nov
%B Month (full) November
%m Month in number (01-12) 07
%y Year (short) 22
%Y Year (full) 2022
%p Time in AM/PM AM
%j Day in number (001-366) 125
%H Hour in number (00-23) 17
%I Hour in number (00-12) 09

 

We can use this to create a well-formatted object as well.

import datetime

current_date_time_object = datetime.datetime.now()

formatted_object = current_date_time_object.strftime("Date-> %d.%m.%Y Time-> %H:%M:%S")  # Format to DD.MM.YYYY and HH:MM:SS  


print(formatted_object)

The output would be:

FUNCTION 2: strptime()

The strptime() function converts a string to a datetime object, but it cannot be just some random string rather it must have its format. The function takes 2 parameters, the string to which we want to convert it to the datetime object and the way to format it.

import datetime

date_string = "25th July 1993"

# Type of the date string is string
print(type(date_string))

date_object = datetime.datetime.strptime(date_string, "%dth %B %Y")

print(date_object)
print(type(date_object))

We get the output as:

What have we learned?

  • How do we work with dates and times in Python?
  • What are the parameters of the datetime class?
  • What is the default value of the timezone parameter?
  • What are some of the codes when working with datetime?
  • What is the difference between strftime and strptime?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments