• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2863
  • Last Modified:

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);
}
0
vmandem
Asked:
vmandem
  • 5
  • 4
  • 3
1 Solution
 
leakim971PluritechnicianCommented:
Check this : http://www.w3schools.com/jsref/jsref_getday.asp

0 for Sunday
6 for Saturday

So try :


function GetCurrentDates(days) {
		var d = 0
		do {
			d++;
			var today = new Date();
			var month = today.getMonth() + d;
			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
	}

Open in new window

0
 
Samuel LiewCommented:
Check this out. It is exactly what you need.
foo49---Get-Working-Day.html
0
 
Samuel LiewCommented:
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());
}

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
leakim971PluritechnicianCommented:
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
	}

Open in new window

0
 
vmandemAuthor Commented:
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
0
 
leakim971PluritechnicianCommented:
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
0
 
vmandemAuthor Commented:
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
0
 
vmandemAuthor Commented:
I beleive currentdate is not actually a date datatype you think it does not recognize the getDay() funciton
0
 
Samuel LiewCommented:
vmandem, have you tried my suggestion? It is exactly what you want, plus I have tested it already.
0
 
Samuel LiewCommented:
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.
0
 
leakim971PluritechnicianCommented:
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
    }

Open in new window

0
 
leakim971PluritechnicianCommented:
Thanks a lot for the points!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now