python timedelta | python datetime tutorials | Third Part
Posted by
Suraj Singh
on March 30, 2017 ·
216 mins read
hii readers,
readers, 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 : # surajsinghbisht054@gmail.com # https://www.bitforestinfo.com # # # Here, I am Using # # Operating System : Ubuntu 16.04 LTS # Python Version : 2.7.12 # Editor : ipython notebook # #
In [2]:
# # Import Module # importdatetime
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 printdt
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)
# # 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 # printt1 # # my second object # t2=datetime.timedelta(hours=92,minutes=28) # # print object # printt2
# # 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. # printage
7949 days, 0:00:00
In [12]:
# # So, readers, Today Tutorials Ends Here. # But readers, 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 :