vmandem
asked on
How to exclude weekends and go previous working day in javascript
I have a function to get current date like below. I pass either 0 or 1 for days to the function and it returns the currentdate or currentdate plus one day. Now I want to excllude weekends, for example if today is saturday or sunday move the current date to Friday. How can I check that in Javascript
function GetCurrentDates(days) {
var today = new Date();
var month = today.getMonth() + 1
var day = today.getDate() + days
var year = today.getFullYear()
var currentdate = (month + "/" + day + "/" + year)
var mycurrentdate = new Date(currentdate);
}
function GetCurrentDates(days) {
var today = new Date();
var month = today.getMonth() + 1
var day = today.getDate() + days
var year = today.getFullYear()
var currentdate = (month + "/" + day + "/" + year)
var mycurrentdate = new Date(currentdate);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'll post the code here for the record.
function getWorkingDay(additionalDays) {
// Check if parameter 'daysFromToday' is correct type
if(!isNaN(parseInt(additionalDays)))
days = parseInt(additionalDays);
else days = 0;
// Get today's date and set new date based on parameter value
var today = new Date();
today.setDate(today.getDate() + days);
// Sun, minus two days to get to Friday
if (today.getDay() == 0) {
today.setDate(today.getDate() - 2);
}
// Sat, minus one day to get to Friday
else if (today.getDay() == 6) {
today.setDate(today.getDate() - 1);
}
// Return required date string
return (today.getMonth() + "/" + today.getDate() + "/" + today.getFullYear());
}
corection for my code :
function GetCurrentDates(days) {
days--;
do {
days++;
var today = new Date();
var month = today.getMonth() + 1;
var day = today.getDate() + days;
var year = today.getFullYear()
var currentdate = (month + "/" + day + "/" + year)
}
while( (currentdate.getDay() == 0) || (currentdate.getDay() == 6) ); // 0 for Sunday, 6 for Saturday
}
ASKER
leakim971
I tried your function and I get an error as object doesnt support this property or procedure. I see that you skipped on of the variable I declared in my original function that I convert the current date to a date.
var mycurrentdate = new Date(currentdate);
In your funciton I think it is not returning as a date. In my original funciton I convert it into a date because I'm doing a comparison after I get the returned date. Can you tell me what is wrong
I tried your function and I get an error as object doesnt support this property or procedure. I see that you skipped on of the variable I declared in my original function that I convert the current date to a date.
var mycurrentdate = new Date(currentdate);
In your funciton I think it is not returning as a date. In my original funciton I convert it into a date because I'm doing a comparison after I get the returned date. Can you tell me what is wrong
You're right, typo error : var mycurrentdate = new Date(currentdate);
you need to add it
What we do is : we add << days >> to current date and at the end we check if the date is a Sunday or a Saturday. If it's the case we loop again and we increment << days >> until we don't get a saturday or sunday
you need to add it
What we do is : we add << days >> to current date and at the end we check if the date is a Sunday or a Saturday. If it's the case we loop again and we increment << days >> until we don't get a saturday or sunday
ASKER
I get error at this line and it says OBJECT DOESN'T SUPPORT THIS PROPERTY OR METHOD
while ((currentdate.getDay() == 0) || (currentdate.getDay() == 6)); // 0 for Sunday, 6 for Saturday
while ((currentdate.getDay() == 0) || (currentdate.getDay() == 6)); // 0 for Sunday, 6 for Saturday
ASKER
I beleive currentdate is not actually a date datatype you think it does not recognize the getDay() funciton
vmandem, have you tried my suggestion? It is exactly what you want, plus I have tested it already.
Not to mention I have added additional error checking if the parameter is not an integer, and also using fewer variables, thus saving memory and increasing processing speed. Also, there are no loops involved, and I have commented every single line of code.
my bad, check this one :
function GetCurrentDates(days) {
days--;
do {
days++;
var today = new Date();
var month = today.getMonth() + 1;
var day = today.getDate() + days;
var year = today.getFullYear()
var currentdate = (month + "/" + day + "/" + year)
var rday = new Date(year, month-1, day);
}
while( (rday.getDay() == 0) || (rday.getDay() == 6) ); // 0 for Sunday, 6 for Saturday
}
Thanks a lot for the points!
foo49---Get-Working-Day.html