Extract Date-From and Date-To in DateRangeField django

62 views
Skip to first unread message

RON MICHAEL

unread,
Feb 16, 2017, 8:31:44 PM2/16/17
to Django users
Hi! In my database I have a DateRangeField. What I want to do is get the date-from and the date-to and store them in a separate variable like example:

date_from =DateRangeField(date_from)
date_to
=DateRangeField(date_to)


of course that code won't work. My data is like this:

'date_range': DateRange(datetime.date(2017, 2, 17), datetime.date(2017, 2, 18), '[)')



How do I get those dates?

Btw, I've tried indexing this, and it doesn't work. I've tried iterating it, but it also doesn't work. 


"Александр Христюхин (roboslone)"

unread,
Feb 17, 2017, 7:13:39 AM2/17/17
to django...@googlegroups.com
Hi, do you mean to store some kind of duration? There's DurationField for that. And you could store datetime.timedelta there. To get timedelta between two datetime.datetime objects, you could subtract ine from another:


from django.utils import timezone as tz

now = tz.now()
day_ago = tz.now() - tz.timedelta(days=1)
duration = now - day_ago  # duration can be stored in DurationField

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/848d9e95-4701-4984-bfee-64526621084e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

RON MICHAEL

unread,
Feb 17, 2017, 9:24:25 AM2/17/17
to Django users
No NOT duration. I simply want to get the date_from and the date_to which is inside the DateRange. 

Melvyn Sopacua

unread,
Feb 17, 2017, 9:40:14 AM2/17/17
to django...@googlegroups.com

On Friday 17 February 2017 06:24:25 RON MICHAEL wrote:

> No NOT duration. I simply want to get the date_from and the date_to

> which is inside the DateRange.

 

You mean the lower and upper.

 

--

Melvyn Sopacua

RON MICHAEL

unread,
Feb 17, 2017, 9:56:17 AM2/17/17
to Django users
Yes that's it! Sorry, I lost the words there :D
Reply all
Reply to author
Forward
0 new messages