Showing posts with label Datetime. Show all posts
Showing posts with label Datetime. Show all posts

Add month value in python

Tuesday, August 6, 2013

Subscribe for future Changes.. Explanation soon..

This is short and sweet method to add a month in a date using relativedelta.
from datetime import datetime
from dateutil.relativedelta import relativedelta

date_after_month = datetime.today()+ relativedelta(months=1)
print 'Today: ',datetime.today().strftime('%d/%m/%Y')
print 'After Month:', date_after_month.strftime('%d/%m/%Y')
Output:
Today: 01/03/2013
After Month: 01/04/2013

Calculate time difference in minutes

Thursday, June 13, 2013


Below code will explain how to find a difference in the minute,
if you have two dates, then you can find the difference of the time using divmode function of python
 >>> import datetime  
 >>> end = datetime.datetime.now()  
 >>> start = datetime.datetime.now()  
 >>> diff  
 datetime.timedelta(0, 7, 424199)  
 >>> diff = start - end  
 >>> divmod(diff.days * 86400 + diff.seconds, 60)  
 (0, 7) # 0 minutes, 7 seconds