Find number of days between two date excluding weekend days and display the number in text field

132 views
Skip to first unread message

Eugene TUYIZERE

unread,
Jun 15, 2021, 2:00:21 AM6/15/21
to django...@googlegroups.com
Team,

I have date1 and date2 fields. I am able to calculate number of days between the two dates using relativedelta as shown below:

date1 = datetime.datetime.strptime(str(end_date), '%Y-%m-%d')
date2 = datetime.datetime.strptime(str(start_date), '%Y-%m-%d')
r = relativedelta.relativedelta(date1, date2)
n_days=r.days
What I want now is calculating the days but excluding weekends days. Again I want to pick the second date and get the number of days (date2-date1) in the text field directly.

Please help.

Thanks
--
Eugene


Nikeet NA

unread,
Jun 15, 2021, 5:13:15 AM6/15/21
to Django users
You need to loop through the dates maintain a count variable get the weekday for every datetime object with date.weekday() exclude as per your need for days you dont want to count.

Eugene TUYIZERE

unread,
Jun 15, 2021, 6:27:07 AM6/15/21
to django...@googlegroups.com
Dear NA,

I accomplished this with numpy. How can I get the value (difference between the two dates) when I select the second date for example in the text field? I know Javascript  can be used but how?

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/16ba7533-93ba-4925-9f93-e430d7128342n%40googlegroups.com.


--
TUYIZERE Eugene

Msc Degree in Mathematical Science

African Institute for Mathematical Sciences (AIMS Cameroon)
Crystal Garden-Lime, Cameroon


Bsc in Computer Science

UR-Nyagatare Campus

Email: eugene....@aims-cameroon.org
           eugenet...@gmail.com

Tel: (+250) 7 88 26 33 38, (+250) 7 22 26 33 38

Nikeet NA

unread,
Jun 15, 2021, 8:37:12 AM6/15/21
to Django users
You need to hit an ajax request to the server on the api when there is change event on the field from were you are taking the second date as input send both first and second date as data in post request to the backend and send the days as response 
Reply all
Reply to author
Forward
0 new messages