Python Datetime Operations

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-datetime-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.

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.

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 Prevent Sql Injection in Php Applications?

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.

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.

Time Delta

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.

Date Arithmetic

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

LEARN MORE  Introduction To Memcached with Telnet and Python

Comparing

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.

Combining Dates and Time

Formatting

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

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

Leave a Reply

Your email address will not be published.