57
Try this:
import datetime
pub = lastItem.pub_date
end_date = datetime.datetime(pub.year, pub.month, pub.day)
65
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)
- [Django]-Django admin and showing thumbnail images
- [Django]-How to convert JSON data into a Python object?
- [Django]-Custom Filter in Django Admin on Django 1.3 or below
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)
- [Django]-What's the best way to start learning django?
- [Django]-How can I create custom page for django admin?
- [Django]-Explicitly set MySQL table storage engine using South and Django
12
For tz-aware dates it should be:
datetime.combine(dt.date(), datetime.min.time(), dt.tzinfo)
- [Django]-How can I call a custom Django manage.py command directly from a test driver?
- [Django]-How to expire Django session in 5minutes?
- [Django]-OneToOneField and Deleting
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
.
- [Django]-Naming convention for Django URL, templates, models and views
- [Django]-Django edit form based on add form?
- [Django]-How to get username from Django Rest Framework JWT token
Source:stackexchange.com