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
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.
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.
-- 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= ...
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.
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
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)))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With