Date and Time

Date and Time

  Machine Learning in Python

Summary : Date and time calculation can get pretty cumbersome if we have to do it ourselves without help from external libraries. Python has standard libraries that let us deal with this.


Let’s first deal with just date ( not time )  

Date Object

from datetime import date 
today =, 4, 2) 

The function today returns today’s date. What is tomorrow ?

from datetime import timedelta 
tomorrow = today + timedelta(days = 1 ), 4, 2) 

You can also add weeks, days, hours, minutes, seconds or even microseconds. The module timedelta can be used to add a particular delta unit of time – it could be weeks or days or hours, even up to microseconds.

You can extract specific information like day or month or year from the date object
# and by default
# today.month
# today.year

What if you wanted to subtract one date from another ? Say, how many days between today and 1 week from now ? You can use the  operator, just like how you would subtract numbers.

today =
week  = today + timedelta(weeks = 1) 

You can create date objects for a particular date. For example, say my birthday is on June 15, 1985. We can put it is as

birthday = date(1985,6,15) 

Now that we know how to create date objects from scratch, let’s do a small calculation. How many days till my next birthday ?  

How many days till my next birthday

We know each of the following

– today

– birthday

Now, we have to calculate days until my next birthday. This requires a bit of manipulation, especially with the year. Since the birthday is way in the past, we need to reset it to this year (or the next ) depending on if the birthday has already passed or is it still ahead

next_birthday = date(today.year,birthday.month, 

Has it already passed this year or is it still ahead ?

# birthday already passed this year. So, calculate until the next year 
if next_birthday < today :     
    next_birthday = date(today.year+1, birthday.month, 

Now, all we have to do is calculate the delta days.

number_days = next_birthday - today 

Time Object

Every computer measures time in seconds since a starting point. Typically, its 1970. The time ( ) functions gives the number of seconds since.

import time 

This simple function can be pretty useful in python programs, specifically to calculate how long it takes for a piece of code to execute.

before = time.time() 
sum = 0 
for i in range(10000000) :     
    sum = sum + i 
after = time.time() 
time_taken = after - before 

OK.. That was summing up 10 million numbers and it took 1.8 seconds. What if we increase it to a billion ?

before = time.time() 
sum = 0 
for i in range(1000000000) :     
    sum = sum + i 
after = time.time() 
time_taken = after - before 

Almost a 100 times. Looks correct.  

Datetime Object

datetime object contains info on both date and time.

import datetime 
datetime.datetime(2019, 4, 2, 13, 58, 20, 428414) 

That’s year,month, day,hour,minute,seconds, microseconds. You can very well construct a datetime object using the same parameters.

now = datetime.datetime(2019, 4, 2, 13, 58, 20, 428414) 
datetime.datetime(2019, 4, 2, 13, 58, 20, 428414) 

Of course, you can also extract all of the info back from the datetimeobject. 

Of course, you can also extract all of the info back from thedatetime object.

and similarly

  • now.month
  • now.year

and similarly

  • now.second
  • now.microsecond
  • now.microsecond

you can even extract specific dateand time objects., 4, 2) 
now.time(), 4, 2) 

A simple example where you can apply this is if you are writing a event management app or a calendar app. For example, on top of other things related to the event, one of the key parameters is the start date/time and end date/time.

Another interesting use of datetime is to extract date and time from text and use it to do intelligent analysis. To extract text and convert it into date or time objects, you can use the strptime function of datetime object.

For example, if you have a text column in a dataset that contains date in the format JAN-01-1980, you can use the strptime function as follows.

from datetime import datetime  
d = "JUN-15-1980" 
date = datetime.strptime(d, "%b-%d-%Y") 
datetime.datetime(1980, 6, 15, 0, 0) 

Once you have the date string into the datetime object, lot of information can be extracted just from this date+time object. For example, day of the week . In time series analysis of stocks, this would be useful in analyzing which days of the week have higher trade.

  • 0- monday
  • 1- tuesday
  • ..
%d bloggers like this: