Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 677
Designer023 Avatar asked Dec 02 '11 18:12

Designer023


People also ask

How do you get 12 time in Python?

strptime() to parse the 24-hour string representations into a time. struct_time object, then use library function time. strftime() to format this struct_time into a string of your desired 12-hour format. %I is a directive that tells Python to give the hour in the 12-hour format.


2 Answers

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

from datetime import date, datetime, time
pub_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)

like image 67
raman Avatar answered Oct 09 '22 23:10

raman


Try this:

import datetime
pub = lastItem.pub_date
end_date = datetime.datetime(pub.year, pub.month, pub.day)
like image 26
mipadi Avatar answered Oct 10 '22 01:10

mipadi