Python Date Time Operations Tutorial with Examples – POFTUT

Python Date Time Operations Tutorial with Examples

Python datetime library provides a lot of different functionalities to operate with date and time values. Python datetime library can be found later versions from 2.3. There are datetime library-related libraries like time and calendar if you are interested in a specific issue.

Time

Time values are represented with time class. Time class gives the ability to hold values like an hour, minute, second, microsecond. Time values also hold time zone information.

Create Time Object With Current Time

We will create a new time object with the current time. Time objects will hold values like hour, minute, second, time zone values.

Create Time Object With Current Time

Create Time Object With Current Time

Time Object Minimum and Maximum Values

Time object has limits to express values. Below maximum and minimum values are printed by using min and max properties.

Time Object Minimum and Maximum Values

Time Object Minimum and Maximum Values

Create Time Object Providing Values

A new time object can be created by providing minute, second and other related values with parameters.

Create Time Object Providing Values

Create Time Object Providing Values

Date

Date class provides to hold information about the year, month, day. today function can be used to initialize new date object like below.

LEARN MORE  How To Navigate, List Files and Directories In Windows Command Line With Dir?

Date

Date

In this example, we print date-time information as a tuple with timetuple function.

Date Object Minimum and Maximum Values

Here are date object minimum and maximum values.

Date Object Minimum and Maximum Values

Date Object Minimum and Maximum Values

Copy Date Instance With New Values

Existing date instance or object can be copied to the new date object by using the replace() function. Replace function will replace if a property and value are given below.

Copy Date Instance With New Values

Copy Date Instance With New Values

Time Delta Or Time Difference

Time delta can be used to get the difference between two date and time objects.

We define two date object 1.1.2015 and 1.1.2016. Then we get time delta of two date objects and print it.

Time Delta

Time Delta

Date Arithmetic

Arithmetic operators can be used to operate on date and time objects. We can sum these time objects like below.

Date Arithmetic

Date Arithmetic

Comparing Date and Time

Comparing date and time objects are similar to primitive data types like integer and string. We can compare two objects with regular compare keywords and operators.

Comparing Date and Time

Comparing Date and Time

Formatting

While printing to the stdout or other direction date and time formats can be formatted with strftime() function like below.

LEARN MORE  How To Learn Python Programming?

Combining Dates and Time Formatting

Combining Dates and Time Formatting

Leave a Reply

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