Python Datetime Module

There is a Python module which is comprised of various functions to deal, manipulate, and work with dates and date objects. This module is known as the Python datetime module. We can simply start working with dates by importing this module and using the functions and classes it provides.

In this module, we will learn about how to solve the problems when it comes to a date in Python, covering the following topics:

Go through this Python Course in London to get a clear understanding of Python!

Current Date and Time with Datetime in Python

Let us see how with the help of Python datetime module and a simple line of code we can get the current date in Python.

Python Datetime Example:

#import datetime python
import datetime
a = datetime.datetime.now()
print(a)

When the above code block is executed, it will display the current date (containing year, month, day, hour, minute, second, and microsecond in the same order). In the ‘datetime.datetime’ statement, the first datetime is the module and the second one is the datetime class inside of the datetime method which contains information from both date and time objects.

We can also display just one of the attributes of date in python, such as, just the year.

Example:

import datetime
a = datetime.datetime.now()
print(a.year)

Learn more about Python from this Python Training in New York to get ahead in your career!

Creating Date Objects in Python

In certain cases, we will want to create a custom date instead of just displaying the current date. In those cases, we can use the datetime() class constructor of the Python datetime module.

This Python datetime() class constructor requires three parameters to create a date, that is, year, month, and day.

Example:

import datetime
a = datetime(2050,11,11)
print(a)


Output:
2050-11-11

The Python datetime() class constructor also takes parameters for time and timezone, which are hour, minute, second, microsecond, and tzone. But these parameters are optionally and rarely used. The default value of these parameters is set to 0.

The strftime() Method in Python Datetime

The datetime module in Python also has a method to convert the date objects into readable strings. So, we can display the month or day in a readable string format instead of numerical representation.

Example:

import datetime
a = datetime.datetime(2020,6,19)
print(a.strftime(“%d”), a.strftime(“%B”))


Output:
19, June

Now, we might be wondering, how do we specify that we want to display the month in a readable string format and what ‘%B’ and ‘%d’ are. Well, Python uses C style formatting to generate new strings. These are called format codes. There are different format codes for different attributes of the date object.
Following table contains a list of all legal format codes and their short descriptions:

Format codeDescriptionExample
%aShort version of weekdayWed
%AFull version of weekdayWednesday
%wWeekday as a number starting from 0 to 6, where 0 is Sunday3
%dDay of the month in numerical format starting from 01 to 3115
%bShort version of month nameDec
%BFull version of month nameDecember
%mMonth as a number starting from 01 to 1212
%yShort version of a year, i.e., without indicating the century19
%YYear in full version2019
%HHour in 24-hour format starting from 00 to 2317
%IHour in am-pm format; 00–1205
%pAM/PMPM
%MMinute starting from 00 to 5941
%SSecond starting from 00 to 5908
%fMicrosecond starting from 000000–999999054647
%ZTimezoneCST
%jDay of the year in numerical format, starting from 001 to 366365
%UWeek of the year in numerical format, starting from 00 to 53, where Sunday is the first day of the week52
%WWeek of the year in numerical format, starting from 00 to 53, where Monday is the first day of the week52
%cLocal version of date and timeMon Mar 04 17:41:00 2018
%xLocal version of date03/04/19
%XLocal version of time19:21:00

Interested in learning Python? Check out the Python Training in Sydney!


We can use any of these codes with the strf method to display their respective values. The strftime method can take one or more than one format codes as argument, as shown in our example, where we have used ‘%B’ to display the full version of the month name in a readable string format and ‘%d’ to display the day of the month in a numerical format.

With this we come to the end of this module in Python Tutorial.Here we have learnt about dealing with date and time in Python with the help of datetime module. Now, if you want to know, how python can be used for data science, you can go through this blog on python for data science.

Further, check out our offers for Python training Course and also refer to the trending Python interview questions prepared by the industry experts.

Leave a Reply

Your email address will not be published. Required fields are marked *