Odoo Help


How to exclude specific day between two date in calculation ?

Ankit H Gandhi(AHG)
on 11/3/15, 5:16 AM 1,070 views

Hello People,

I want to total number of days from two dates. but there is one condition to count total number of day but excluded(ignore) specific day ?


I have below dates  

start date: 01/01/2015

end date: 31/03/2015

Here I want total number of days between start date and end date, but not including " Tuesday " in total number of days.

Thanks in Advance.



| 6 5 7
Tbilisi, Georgia
On 11/3/15, 6:35 AM

@Ankit, here is brute-force version:

from datetime import datetime, date, timedelta

date_start = datetime.strptime('2015-01-01','%Y-%m-%d').date()
date_end = datetime.strptime('2015-03-31','%Y-%m-%d').date()

delta_day = timedelta(days=1)

days = {'mon':0,'tue':1,'wed':2,'thu':3,'fri':4,'sat':5,'sun':6}

dt = date_start

while dt <= date_end:
if dt.weekday() != days['tue']:
dt += delta_day

But suggestion of @Drees is much more lightweight and should be executed faster for large periods, if you'll get it worked.


slightly modified approach suggested by @Drees, if we make sure day count starts at the weekday we are interested in (by "removing" days before first occurrence of the target day i.e. weekday to be excluded), then total count of weekday occurrence will be:

# total_days  -- days between start_date (inclusive) and and_date (inclusive)
# days_before -- days from total_days period, that are before first occurrence of the weekday to be excluded

weekday_count = (total_days - days_before) / 7

if (total_days - days_before) % 7:
weekday_count = weekday_count + 1

days_wit_excluded_target_day = total_days - weekday_count

If we're agree that above code may calculate correctly the day count in [start_date, end_date] period with a target_weekday excluded, then there is a corresponding code:

version with calculation

from datetime import datetime

date_start_val = '2015-01-01' # start date (inclusive)
date_end_val = '2015-03-31' # end date (inclusive)

date_start = datetime.strptime(date_start_val,'%Y-%m-%d').date()
date_end = datetime.strptime(date_end_val,'%Y-%m-%d').date()

days = {'mon':0,'tue':1,'wed':2,'thu':3,'fri':4,'sat':5,'sun':6}

total_days = (date_end - date_start).days + 1

first_weekday = date_start.weekday()
target_weekday = days['tue']

if target_weekday == first_weekday:
days_before = 0
elif target_weekday < first_weekday:
days_before = 7 - first_weekday + target_weekday
days_before = target_weekday - first_weekday
weekday_count = total_days - days_before
if weekday_count > 0:
weekday_count = weekday_count/7 + (weekday_count%7 and 1 or 0)
weekday_count = 0

day_count = total_days - weekday_count

this version should be much faster on large periods then other versions.

Thanks for your help @ Temur !!! It is fully working...+1

Ankit H Gandhi(AHG)
on 11/3/15, 7:26 AM

Please see the last version that I've added after update. it's a recommended version.

on 11/3/15, 9:40 AM

wrapped code into the python function, see a gist

on 11/3/15, 10:31 AM

Sorry @ Temur Using updated code I am not get perfect total days. I am using below date for count date_start_val = '2015-07-04' date_end_val = '2015-07-05' I got total number of days in 0 (zero), but actually total number of days will be 2 (Two) . Please advice on it.

Ankit H Gandhi(AHG)
on 11/4/15, 2:07 AM

<=0 as there is not Tuesday at all in date range ['2015-07-04','2015-07-05' ] then day_count = 0 were executed under else clause in the above code fragment, instead of weekday_count = 0. It's corrected now in the answer and function in gist is updated accordingly.

on 11/4/15, 2:31 AM

Thanks again now it's work fine..@ Temur

Ankit H Gandhi(AHG)
on 11/4/15, 3:17 AM

You're welcome

on 11/4/15, 3:18 AM

Drees Far

--Drees Far--
Drees Far
| 5 2 6
Tunis, Tunisia
--Drees Far--
Drees Far
On 11/3/15, 5:31 AM

Hi Ankit;

What i propose to you is to calculate the total of days between start_date and end_date.

And then, to get your 'Tuesday' day you have to divide the total number by seven.

And you have to get the day of the first_date as shown by this code:


>>> import datetime

>>> datetime.datetime.today()

datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)

>>> datetime.datetime.today().weekday()


Where weekday Returns the day of the week as an integer, where Monday is 0 and Sunday is 6.


Total_date = 29

29/7 = 4 + 1 so there is 4 tuesday and 1 < 3 if the start_date is monday

so total_date becomes 25.

Hope this may help you :)


Thanks for you quick response @ Dress Far +1 I had little bit confusion with your code but @ Temur solved out confusion

Ankit H Gandhi(AHG)
on 11/4/15, 4:50 AM

Akhil P Sivan

--Akhil P Sivan--
Akhil P Sivan
| 5 3 7
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 11/3/15, 6:24 AM

Hi Ankit,

If the start date and end date are fields of type date, you can try the following code:

For example, to avoid tuesdays and get the day count:

import dateutil.parser
import datetime
from openerp import models, fields

class your_class(models.Model):
_name = "your.model"

def your_function(self):
days_count = 0
while (start_date < end_date):
day = dateutil.parser.parse(start_date).date().weekday()
if day != 1:
days_count += 1
start_date = start_date + datetime.timedelta(days=1)

Here days_count will give the no. of days between two dates avoided tuesday

if the start_date and end_date are not date fields, you need convert to date type like this:

s_date = dateutil.parser.parse(start_date).date()

Thanks @ Akhil.. It is work fine. +1

Ankit H Gandhi(AHG)
on 11/3/15, 7:21 AM
Rabie Sakhri
On 11/8/15, 5:32 AM

Thanks for this help

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 11/3/15, 5:16 AM
Seen: 1070 times
Last updated: 11/8/15, 5:32 AM