Dates difference excluding weekends
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The DATEDIFF function allows to find out the dates difference between the two dates. I am just wondering if there is any easy way to find the dates difference excluding weekends? , Here is another one solution: set datefirst 1
;with cte as
(select cast('20111101' as datetime) as dt union all select dt+1 from cte where dt<'20111201')
select count(*) as cnt from cte
where dt between '20111123' and '20111129'
and datepart(dw, dt) < 6
|
R: Difference between two dates excluding weekends and given list of holidays specific to state
Date : March 29 2020, 07:55 AM
Hope that helps Here is an inelegant solution to my question. hope you find this useful. library('dplyr')
holiday <- data.frame(h = as.Date(c("2016/05/3", "2016/05/3"),'%Y/%m/%d'),
s = c('state1','state2'),
stringsAsFactors=FALSE
)
d <- data.frame(d1 = as.Date(c('2016/05/2','2016/05/2'),'%Y/%m/%d'),
d2= as.Date(c('2016/05/10','2016/05/10'),'%Y/%m/%d'),
s = c('state1','state3'),
stringsAsFactors=FALSE)
state <- as.character(unique(d$s))
#function to exclude weekends and holidays
f <- function(a, b, h) {
d <- seq(a, b, 1)[-1]
sum(!format(d, "%u") %in% c("6", "7") & !d %in% h)
}
vf <- Vectorize(f, c("a", "b"))
#Function to calculate days excluding weekends and holidays by state
#state is done iteratively
datalist = list()
for (i in 1:length(state)) {
# ... make some data
#extract holidays by state as vectors
holi <- holiday %>%
filter(s== state[i]) %>%
select(h) %>%
pull()
#create new data by state
dat <- d %>%
filter(s == state[i])
dat$diff <- with(dat,vf(d1, d2, holi))
datalist[[i]] <- dat # add it to your list
rm('dat')
}
new_df = dplyr::bind_rows(datalist)
d1 d2 s diff
1 2016-05-02 2016-05-10 state1 5
2 2016-05-02 2016-05-10 state3 6
|
Generate a list of weekly business dates between two dates excluding weekends and holidays
Date : March 29 2020, 07:55 AM
this will help It looks like you already have everything you need, but holidays is never set to your Holidays. Change the line below so that holidays is set to null. Then it will hit the null check in IsHoliday and be set properly. var holidays = new List<DateTime>();
var holidays = null;
|
I want to get all dates between two dates excluding weekends in jquery
Tag : jquery , By : user165871
Date : March 29 2020, 07:55 AM
To fix the issue you can do The below code will iterate across all days in a given time frame and push the dates to an array if the day isn't a Saturday or Sunday. It doesn't account for public holidays and uses Vanilla JS (no jQuery needed.) As you can see, I use .getDay() to determine whether what day of the week a given date is and check that it doesn't equal 6 (Saturday) or 0 (Sunday). function getBusinessDays(start_date, end_date)
{
var start = new Date(start_date);
var end = new Date(end_date);
var current = start;
var dates = [];
while(current<=end) // while the "current" date is <= the end date
{
if(current.getDay() !== 6 && current.getDay() !== 0) // check if not Sat or Sun
{
dates.push(new Date(current)); // add date to array
}
current.setDate(current.getDate() + 1); // increase current date by 1 day
}
return dates;
}
var businessDays = getBusinessDays('10/01/2019', '11/29/2019');
console.log(businessDays);
|
Difference between two dates excluding weekends and given list of holidays in R
Tag : r , By : user133629
Date : March 29 2020, 07:55 AM
|