Skip to Content
Menu
This question has been flagged
2 Replies
4414 Views

for example 

I have Saturday and Sunday it's string value so i get Saturday & Sunday ....

day can be change some time user select Friday, Monday ..... (any day user can select)

(i want to count user selected day in given month)

and i get value from user 01-08-2019 to 31-08-2019

i want to count how many Saturday & Sunday between date from to date end...

for example result should be like that 8 

any helping code ? 

Avatar
Discard
Best Answer

Hello,

Please Check this code, 


from_date = fields.Date("From Date")
to_date = fields.Date("To Date")
month_days = fields.Selection([('Monday','Monday'),('Tuesday','Tuesday'),
('Wednesday','Wednesday'),('Thursday','Thursday'),
('Friday','Friday'),('Saturday','Saturday'),
('Sunday','Sunday')], "Day")
day_count = fields.Integer("Number Of Days")


@api.onchange('from_date', 'to_date', 'month_days')
def onchange_count_days(self):
count_days = 0
if self.from_date and self.to_date and self.month_days:
day_from = datetime.strptime(self.from_date, "%Y-%m-%d")
day_to = datetime.strptime(self.to_date, "%Y-%m-%d")
nb_of_days = (day_to - day_from).days + 1
for day in range(0, nb_of_days):
if day == 0 and str(day_from.strftime('%A')) == str(self.month_days):
count_days += 1
else:
month_date = day_from + relativedelta(days=day)
if str(month_date.strftime('%A')) == str(self.month_days):
count_days += 1
self.day_count = count_days

Hope it will work for you,

Thanks

Avatar
Discard
Best Answer

from calendar import monthrange
def number_of_days_in_month(year=2019, month=2):
return monthrange(year, month)[1]
# Usage example:
print(number_of_days_in_month(2019, 2)) # Prints 28

Avatar
Discard
Related Posts Replies Views Activity
1
Oct 24
4589
3
Jan 24
1550
1
Jan 20
6270
0
May 18
2222
1
Oct 22
1196