Python Trick: Subtracting Dates

I’m not the Python genius I want to be, so here’s this, for future reference:

How to subtract one date from another, and get the result in days:

from datetime import date
(date(2008, 3, 31) - date(2005, 2, 1)).days

A simple application:

party = date(2008, 6, 1)
(party -

3 Responses to Python Trick: Subtracting Dates

  1. gr8dude says:

    Nice code highlighting thingie!

  2. kenny says:

    listen u have no idea how much u just helped me, honstly search the internet and you will find all this crap about date and time, all i needed was this, completely simple!

    you just helped me pass my year of uni.

    so greatful!

  3. Salil says:

    dude i could just kiss you right now!!! you have no idea how long i’ve searched for this!! thanks a bunch!!

%d bloggers like this: