python timedelta | python datetime tutorials | Third Part

Namaste Friends,




                             Friends, This Is Our Third Part of Python Date And Time Tutorials Series. And In This Post, I Going To Show You How To Use Python DateTime Module With Practical Examples. 


But First If You Are New Visitor Then Click Here For Second Part

or 

Check Our Blog Post Index. 



So, let's start with some practical usages examples.


datetime_part_3 slides
In [1]:
#!/usr/bin/python
#
# ============================================
#       PYTHON DATETIME MODULE TUTORIAL
# ============================================
#
# Author : SSB
#          surajsinghbisht054@gmail.com
#          https://bitforestinfo.blogspot.com
# 
#
# Here, I am Using
# 
# Operating System : Ubuntu 16.04 LTS
# Python Version   : 2.7.12
# Editor           : ipython notebook
#
#
In [2]:
#
# Import Module
#
import datetime
In [3]:
#
# datetime.timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
#
#        A duration expressing the difference between two date, 
#        time, or datetime instances to microsecond resolution.
#
# let me show you how to use it
#
dt = datetime.timedelta(days=6, hours=8092,minutes=5128)
#
# as you can see here i enter 6 days, 8092 hours and
# 5128 minutes
#
# and now, let check output
print dt
346 days, 17:28:00
In [4]:
#
# hmm, as you can see we get well managed output.
#
# let's try more things
#
print "[1] Total Days    : ", dt
print "[2] Total Seconds : ",dt.total_seconds()
[1] Total Days    :  346 days, 17:28:00
[2] Total Seconds :  29957280.0
In [5]:
#
# Passing All Arguments one by one
#
print "[1] Microseconds ",datetime.timedelta(microseconds=1)
print "[2] Milliseconds ",datetime.timedelta(milliseconds=1)
print "[3] Seconds      ",datetime.timedelta(seconds=1)
print "[4] Minutes      ",datetime.timedelta(minutes=1)
print "[5] Hours        ",datetime.timedelta(hours=1)
print "[6] Days         ",datetime.timedelta(days=1)
print "[7] Weeks        ",datetime.timedelta(weeks=1)
[1] Microseconds  0:00:00.000001
[2] Milliseconds  0:00:00.001000
[3] Seconds       0:00:01
[4] Minutes       0:01:00
[5] Hours         1:00:00
[6] Days          1 day, 0:00:00
[7] Weeks         7 days, 0:00:00
In [6]:
#
# Now, Here, I am going to shw you valid mathematics
# function with timedetla class
#
# but for this first take two object
# my first object
t1 = datetime.timedelta(days=6)
#
# print object
#
print t1
#
# my second object
#
t2 = datetime.timedelta(hours=92,minutes=28)
#
# print object
#
print t2
6 days, 0:00:00
3 days, 20:28:00
In [7]:
#
# addition
#
print t1 + t2
#
# substraction
#
print t1 - t2
#
# multiplication
#
print t1 * 15
#
# division
#
print t1 / 2
9 days, 20:28:00
2 days, 3:32:00
90 days, 0:00:00
3 days, 0:00:00
In [8]:
#
# Comparing Values
#
#  more than 
#
print "is t1 is smaller than t2 : ", t1 < t2
#
# less then
#
print "is t1 is bigger than t2  : ", t1 > t2
is t1 is smaller than t2 :  False
is t1 is bigger than t2  :  True
In [9]:
#
# and in the end of this tutorial,
# let me show you an example of age 
# calculator.
#
# first take date for calculation
#
dt=datetime.date(1995,6,25)
#
# second, take current date for comparision
#
now = datetime.date.today()
In [10]:
#
# now, substract first date from current date 
#
age = abs(now-dt)
In [11]:
#
# here, we got our age.
#
print age
7949 days, 0:00:00
In [12]:
#
# So, Friends, Today Tutorials Ends Here.
# But Friends, Don't Worry, In Next Tutorial.
# i will show you more interesting topic.
#
#
# Reference :
# 
# Python Official Website:
#         https://docs.python.org/2/library/datetime.html
#
#




This Part Ends Here.

If You Like My Tutorials,
Then Subscribe my blog,
like, comment and share.


By :
    SSB

Share this

Related Posts

Previous
Next Post »