django python date time set to midnight django python date time set to midnight django django

django python date time set to midnight


Using datetimes's "combine" with the time.min and time.max will give both of your datetimes.For example:

from datetime import date, datetime, timepub_date = date.today()min_pub_date_time = datetime.combine(pub_date, time.min) max_pub_date_time = datetime.combine(pub_date, time.max)  

Result with pub_date of 6/5/2013:

min_pub_date_time -> datetime.datetime(2013, 6, 5, 0, 0)

max_pub_date_time -> datetime.datetime(2013, 6, 5, 23, 59, 59, 999999)


Try this:

import datetimepub = lastItem.pub_dateend_date = datetime.datetime(pub.year, pub.month, pub.day)


Are you sure you don't want to use dates instead of datetimes? If you're always setting the time to midnight, you should consider using a date. If you really want to use datetimes, here's a function to get the same day at midnight:

def set_to_midnight(dt):    midnight = datetime.time(0)    return datetime.datetime.combine(dt.date(), midnight)