Python Date Time Operations

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-date-time-operations/
PINTEREST
PINTEREST
INSTAGRAM

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 is datetime library related libraries liketime and calender if you are interested specific issue.

Time

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

Create Time Object With Current Time

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

Create Time Object With Current Time

Create Time Object With Current Time

Time Object Minimum and Maximum Values

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

Create Time Object Providing Values

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 year, month,day. today function can be used to initialize new date object like below.

LEARN MORE  How To Control Python For Loop with Break Statement?

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 replace function. Replace function will replace if a property and value is given like 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 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 two 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 object with regular compare keywords and operators.

Comparing Date and Time

Comparing Date and Time

Combining Dates and Time

Formatting

While printing to the stdout or other direction date and time formats can be formatted with strftime.

LEARN MORE  Django Session Variable and Seriliazation

Combining Dates and Time Formatting

Combining Dates and Time Formatting

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-date-time-operations/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)