django python date time set to midnight
Asked Answered
E

5

47

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

Evaevacuant answered 2/12, 2011 at 18:47 Comment(2)
you need a datetime variable initialized to 00:00:00?Lichter
related: How do I get the UTC time of “midnight” for a given timezone?Practically
A
56

Try this:

import datetime
pub = lastItem.pub_date
end_date = datetime.datetime(pub.year, pub.month, pub.day)
Aorist answered 2/12, 2011 at 18:56 Comment(6)
An alternative would be end_date.replace(hour=0, minute=0, second=0).Abet
To ensure it's totally zeroed out, you may need to add microsecond=0 to F.J's suggestion. For instance, when you're zeroing out the time portion of datetime.now().Rajput
@F.J Are you sure? I get "'second' is an invalid keyword argument for this function" (or 'hour', or 'minute') when I do that. And it's a datetime object not a date. Maybe I was doing something wrong tho.Ake
@RThiede datetime.datetime.replace() is working fine for me with second and microsecond keyword arguments, make sure that you have a datetime.datetime instance and not a datetime.date.Abet
@AndrewClark: 1. you forgot microsecond in your first comment. 2. .replace() may return an unnormalized value for an aware datetime object that may introduce ~1h error around DST transitions. Both .combine() and datetime() methods strip the timezone info so that you can add the correct value instead of using by accident a possibly incorrect value returned by .replace()Practically
@AndrewClark - this should be an individual answer, adding the microsecond part as said by anhZecchino
R
67

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 2013-06-05:

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

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

Raimes answered 5/6, 2013 at 13:58 Comment(0)
A
56

Try this:

import datetime
pub = lastItem.pub_date
end_date = datetime.datetime(pub.year, pub.month, pub.day)
Aorist answered 2/12, 2011 at 18:56 Comment(6)
An alternative would be end_date.replace(hour=0, minute=0, second=0).Abet
To ensure it's totally zeroed out, you may need to add microsecond=0 to F.J's suggestion. For instance, when you're zeroing out the time portion of datetime.now().Rajput
@F.J Are you sure? I get "'second' is an invalid keyword argument for this function" (or 'hour', or 'minute') when I do that. And it's a datetime object not a date. Maybe I was doing something wrong tho.Ake
@RThiede datetime.datetime.replace() is working fine for me with second and microsecond keyword arguments, make sure that you have a datetime.datetime instance and not a datetime.date.Abet
@AndrewClark: 1. you forgot microsecond in your first comment. 2. .replace() may return an unnormalized value for an aware datetime object that may introduce ~1h error around DST transitions. Both .combine() and datetime() methods strip the timezone info so that you can add the correct value instead of using by accident a possibly incorrect value returned by .replace()Practically
@AndrewClark - this should be an individual answer, adding the microsecond part as said by anhZecchino
O
23

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)
Olympe answered 2/12, 2011 at 18:55 Comment(4)
for midnight the other way would I use 86400 instead of 0? This looks very promisingEvaevacuant
midnight = datetime.time(23, 59, 59)Olympe
Actually, you probably want to use datetime.datetime.max (or min).Radish
datetime.combine(dt.date(), datetime.min.time()) worked for meHulen
R
13

For tz-aware dates it should be:

datetime.combine(dt.date(), datetime.min.time(), dt.tzinfo)
Redo answered 4/12, 2017 at 11:16 Comment(1)
@LidiyaParshina Is it a better way?Redo
R
1

datetime instance attributes like year, month, day, hour, etc are read-only, so you just have to create a new datetime object and assign it to end_date.

Ridgley answered 2/12, 2011 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.