add time to datetime python

37

from datetime import datetime  
from datetime import timedelta  
  
#Add 1 day  
print datetime.now() + timedelta(days=1)  
  
#Subtract 60 seconds  
print datetime.now() - timedelta(seconds=60)  
  
#Add 2 years  
print datetime.now() + timedelta(days=730)  
  
#Other Parameters you can pass in to timedelta:  
# days, seconds, microseconds,   
# milliseconds, minutes, hours, weeks  
  
#Pass multiple parameters (1 day and 5 minutes)  
print datetime.now() + timedelta(days=1,minutes=5)  
import datetime
delta = datetime.timedelta(
     days=50,
     seconds=27,
     microseconds=10,
     milliseconds=29000,
     minutes=5,
     hours=8,
     weeks=2
)

new_datetime = datetime.now() + delta
# Option 01
my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
my_time = my_time.replace(hour=23, minute=59)
# OPtion 02
my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
my_time += datetime.timedelta(hours=23, minutes=59)

bilalahmed-dev.azeemlab.com

Comments

Submit
0 Comments