Skip to content

Instantly share code, notes, and snippets.

@christophergregory
Created March 18, 2013 15:06
Show Gist options
  • Save christophergregory/5187822 to your computer and use it in GitHub Desktop.
Save christophergregory/5187822 to your computer and use it in GitHub Desktop.
Checks to see if date is within a range
import datetime
# Checks too see if date is within a range
def is_within_date(date, min_date, max_date):
# Convert MM/DD/YYYY string format to datetime date
def convert_string_to_date(date_string):
month,day,year = date_string.split("/")
return datetime.date(int(year), int(month), int(day))
day = convert_string_to_date(date)
day_min = convert_string_to_date(min_date)
day_max = convert_string_to_date(max_date)
delta_min = day - day_min
delta_max = day - day_max
return True if delta_min.total_seconds() >= 0 and delta_max.total_seconds() <= 0 else False
date = "03/15/2013"
min_date = "03/01/2013"
max_date = "04/01/2013"
print is_within_date(date, min_date, max_date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment