Question: i'm trying to get the number of trading dates between two dates which will only exclude the weekends and won't consider any holidays. I'm using Boost and c++11 standards. 

```
using namespace boost::gregorian;
long dateDifference( string start_date, string end_date ) {

            date _start_date(from_simple_string(start_date));
            date _end_date(from_simple_string(end_date));

            long difference = ( _start_date - _end_date ).days();

            return difference;

        }

```

This just returns the number of days between two dates without considering weekends. Can someone point me in the right direction. I can't seem to figure out the solution. 

Thanks,
Maxx

Answer: 
Just run a day iterator and calculate the weekdays manually:

```
#include <boost/date_time.hpp>

using namespace boost::gregorian;

long dateDifference( string start_date, string end_date ) 
{
    date _start_date(from_simple_string(start_date));
    date _end_date(from_simple_string(end_date));

    // counter for weekdays
    int cnt=0;
    for(day_iterator iter = _start_date; iter!=_end_date; ++iter)
    {
        // increment counter if it's no saturday and no sunday
        if(    iter->day_of_week() !=  boost::date_time::Saturday
            && iter->day_of_week() !=  boost::date_time::Sunday)
            ++cnt;
    }
    return cnt;
}

```

Answer ported from this answer: <https://stackoverflow.com/a/7342989/3187827>
I didn't find any O(1) solutions which satisfied me so here is what I did:

```
int get_weekdays_count(const boost::gregorian::date& a,const boost::gregorian::date& b)
    {
        int na=(a<b) ? a.day_of_week().as_number() : b.day_of_week().as_number();
        int diff=(a-b).days();
        if(diff!=0){
            if(diff<0) diff*=-1;
            int rslt=diff/7;       //number of saturdays
            rslt*=2;               // times 2 for sundays
            rslt+= (diff%7) >=(boost::gregorian::Saturday-na)%7 ? 1 : 0; // handle special case for saturdays
            rslt+= (diff%7) >=(boost::gregorian::Sunday-na)%7 ? 1 : 0; //special case for sundays
            return 1+diff-rslt;
        }
        else return (na==boost::gregorian::Saturday || na==boost::gregorian::Sunday) ? 0 : 1;
    };

```

This work even if a > b , just put two separate time and here you go.

Speed in a for loop: 25 nanosec/call on VS2012 Release mode // proc i5 4690K