In [1]:
    
d1 = "10/24/2017"
d2 = "11/24/2016"
max(d1,d2)
    
    Out[1]:
In [2]:
    
d1 - d2
    
    
In [1]:
    
import datetime
d1 = datetime.date(2016,11,24)
d2 = datetime.date(2017,10,24)
max(d1,d2)
    
    
In [4]:
    
print(d2 - d1)
    
    
In [2]:
    
import datetime
century_start = datetime.date(2000,1,1)
today = datetime.date.today()
print(century_start,today)
print("We are",today-century_start,"days into this century")
print(type(century_start))
print(type(today))
    
    
In [6]:
    
print("We are",(today-century_start).days,"days into this century")
    
    
In [7]:
    
century_start = datetime.datetime(2000,1,1,0,0,0)
time_now = datetime.datetime.now()
print(century_start,time_now)
print("we are",time_now - century_start,"days, hour, minutes and seconds into this century")
    
    
In [8]:
    
some_date=datetime.date(2015,2,29)
#some_date =datetime.date(2016,2,29)
#some_time=datetime.datetime(2015,2,28,23,60,0)
    
    
In [3]:
    
century_start = datetime.datetime(2050,1,1,0,0,0)
time_now = datetime.datetime.now()
time_since_century_start = time_now - century_start
print("days since century start",time_since_century_start.days)
print("seconds since century start",time_since_century_start.total_seconds())
print("minutes since century start",time_since_century_start.total_seconds()/60)
print("hours since century start",time_since_century_start.total_seconds()/60/60)
    
    
In [10]:
    
date_and_time_now = datetime.datetime.now()
time_now = date_and_time_now.time()
print(time_now)
    
    
In [11]:
    
today=datetime.date.today()
five_days_later=today+datetime.timedelta(days=5)
print(five_days_later)
    
    
In [12]:
    
now=datetime.datetime.today()
five_minutes_and_five_seconds_later = now + datetime.timedelta(minutes=5,seconds=5)
print(five_minutes_and_five_seconds_later)
    
    
In [13]:
    
now=datetime.datetime.today()
five_minutes_and_five_seconds_earlier = now+datetime.timedelta(minutes=-5,seconds=-5)
print(five_minutes_and_five_seconds_earlier)
    
    
In [14]:
    
time_now=datetime.datetime.now().time() #Returns the time component (drops the day)
print(time_now)
thirty_seconds=datetime.timedelta(seconds=30)
time_later=time_now+thirty_seconds
#Bug or feature?
    
    
    
In [17]:
    
#But this is Python
#And we can always get around something by writing a new function!
#Let's write a small function to get around this problem
def add_to_time(time_object,time_delta):
    import datetime
    temp_datetime_object = datetime.datetime(500,1,1,time_object.hour,time_object.minute,time_object.second)
    print(temp_datetime_object)
    return (temp_datetime_object+time_delta).time()
    
In [18]:
    
#And test it
time_now=datetime.datetime.now().time()
thirty_seconds=datetime.timedelta(seconds=30)
print(time_now,add_to_time(time_now,thirty_seconds))
    
    
In [30]:
    
date='01-Apr-03'
date_object=datetime.datetime.strptime(date,'%d-%b-%y')
print(date_object)
    
    
In [26]:
    
#Unfortunately, there is no similar thing for time delta
#So we have to be creative!
bus_travel_time='2:15:30'
hours,minutes,seconds=bus_travel_time.split(':')
x=datetime.timedelta(hours=int(hours),minutes=int(minutes),seconds=int(seconds))
print(x)
    
    
In [27]:
    
#Or write a function that will do this for a particular format
def get_timedelta(time_string):
    hours,minutes,seconds = time_string.split(':')
    import datetime
    return datetime.timedelta(hours=int(hours),minutes=int(minutes),seconds=int(seconds))
    
In [28]:
    
now = datetime.datetime.now()
string_now = datetime.datetime.strftime(now,'%m/%d/%y %H:%M:%S')
print(now,string_now)
print(str(now)) #Or you can use the default conversion
    
    
In [ ]: