Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Exclude weekends between two Dates

I want to calculate the difference between the two dates but want to exclude the weekends from it . Below is the format of dates :

CreateDate  - 2017-08-29 10:47:00
ResolveDate - 2017-09-23 16:56:00
like image 222
Karan Khanna Avatar asked Sep 24 '17 05:09

Karan Khanna


People also ask

How do you exclude weekends in Python?

Get business days excluding weekends and holidaysThe busday_count() function has the option to add public holidays as well so that it can't get included in the final count of business days.

How do I exclude weekends from two dates?

If you'd like to calculate the difference between two dates while excluding weekends and holidays, use the NETWORKDAYS function instead. This also looks for 3 arguments: the start date, the end date, and optional holidays. Unlike the WORKDAY function, the NETWORKDAYS function does include or count the start day.

How do you calculate the difference between two dates excluding weekends in Python?

-- All dates between 01/07/2021 and 15/07/2021 excluding weekends SELECT CAL_DATE FROM ( SELECT to_date('01/07/2021', 'DD/MM/YYYY') + ROWNUM - 1 AS CAL_DATE FROM ALL_OBJECTS WHERE ROWNUM <= to_date('15/07/2021', 'DD/MM/YYYY') - to_date('01/07/2021', 'DD/MM/YYYY') + 1) WHERE to_char(CAL_DATE, 'DY', 'NLS_DATE_LANGUAGE= ...

How do you separate weekends and weekdays in Python?

We can use the weekday() method of a datetime. date object to determine if the given date is a weekday or weekend. Note: The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the date(2022, 05, 02) is a Monday.


3 Answers

You can use numpy.busday_count:

from datetime import datetime
import numpy as np

create_date = "2017-08-29 10:47:00"
resolve_date = "2017-09-23 16:56:00"

create_datetime = datetime.strptime(create_date, '%Y-%m-%d %H:%M:%S')
resolve_datetime = datetime.strptime(resolve_date, '%Y-%m-%d %H:%M:%S')

print(f"The difference in days is: {(resolve_datetime - create_datetime).days}")
print(f"The difference in business days is: {np.busday_count(create_datetime.date(), resolve_datetime.date())}")

Output:

The difference in days is: 25
The difference in business days is: 19
like image 85
Sash Sinha Avatar answered Oct 06 '22 13:10

Sash Sinha


One more python way using isoweekday():

import datetime, pprint

# isoweekday: Monday is 1 and Sunday is 7
start_date = datetime.date(2017, 10, 1)
end_date = datetime.date(2017, 12, 31)
days = end_date - start_date
valid_date_list = {(start_date + datetime.timedelta(days=x)).strftime('%d-%b-%Y')
                        for x in range(days.days+1)
                        if (start_date + datetime.timedelta(days=x)).isoweekday() <= 5
                       }
print("Business Days = {}".format(len(valid_date_list)))
like image 43
Vikas Avatar answered Oct 06 '22 12:10

Vikas


Run a while loop that keeps adding a timedelta of +1 day to create_date. Keep track of weekday vs. weekend in a separate counter.

like image 1
J_H Avatar answered Oct 06 '22 14:10

J_H