Solved

How to exclude weekends and go previous working day in javascript

Posted on 2010-08-31
12
1,762 Views
Last Modified: 2013-11-11
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
Comment
Question by:vmandem
  • 5
  • 4
  • 3
12 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 75 total points
ID: 33571507
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
 
LVL 14

Expert Comment

by:sam2912
ID: 33573704
Check this out. It is exactly what you need.
foo49---Get-Working-Day.html
0
 
LVL 14

Expert Comment

by:sam2912
ID: 33573728
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 82

Expert Comment

by:leakim971
ID: 33575686
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
 

Author Comment

by:vmandem
ID: 33577619
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33577687
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
 

Author Comment

by:vmandem
ID: 33577898
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
 

Author Comment

by:vmandem
ID: 33577983
I beleive currentdate is not actually a date datatype you think it does not recognize the getDay() funciton
0
 
LVL 14

Expert Comment

by:sam2912
ID: 33578223
vmandem, have you tried my suggestion? It is exactly what you want, plus I have tested it already.
0
 
LVL 14

Expert Comment

by:sam2912
ID: 33578266
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33578277
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33641460
Thanks a lot for the points!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question