Search code examples
pythondjangodatedatetimedate-manipulation

django python date time set to midnight


I have a date time of my django object but it can be any time of the day. It can be at any time through the day, but I need to set my time to 00:00:00 (and another date to 23:59:59 but the principle will be the same)

end_date = lastItem.pub_date

currently the end date is 2002-01-11 12:34:56 What do I need to do to get this to change it to 00:00:00?

i tried:

end_date.hour = '00'

but got: 'datetime.datetime' object attribute 'time' is read-only


Solution

  • Try this:

    import datetime
    pub = lastItem.pub_date
    end_date = datetime.datetime(pub.year, pub.month, pub.day)