0

i'm working on site for renting rooms. User picks 2 dates(UserStartDate & UserEndDate).

with this python code i gonna get number of days in his date range:

user_date_range = [endUser - timedelta(i) for i in range((endUser - startUser).days+1)] 
user_range_num_days = len(user_date_range)

and i have a day price for room: 20$

but due to lack of proficiency in Django,I can't figure out how to calculate user price according to his date range. And where it should be done.

hope for your help.

  • I think you are confused about Django-- thats a web framework. Your problem is python.. research using timedelta. And subtracting two dates. – Merlin May 27 '16 at 19:09

2 Answers2

1

It doesn't have anything to do with django but rather python. I assume user_start_date and user_end_date are both python datetime.date or datetime.datetime objects, then you could do:

num_days = (user_end_date - user_start_date).days
total_price = num_days * 20
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • thank you man i'll use it. But I want to know where this logic should be implemented. – filmincer May 27 '16 at 19:15
  • Wherever you want to calculate the price apparently. Where does that happen? – Shang Wang May 27 '16 at 19:25
  • So far, I meet user_end_date & user_start_date (sent by GET request ) via **def get()** in my View. In the future want to dynamically reload price text via AJAX, so the user can see his calculated price instantly. – filmincer May 27 '16 at 19:36
  • Then do it in the ajax call, right? http://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications – Shang Wang May 27 '16 at 19:45
0

https://docs.python.org/2/library/calendar.html

A calendar is necessary as you should be aware that not all months have the same amount of days in them. itermonthdates(year, month) returns an iterator for all days in the month. Run through that iterator and increment a count for every date match within the range. Of course if the end date extends into the next month keep the same counter.

ARoss
  • 23
  • 1
  • 7