Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to exclude weekends and go previous working day in javascript

Posted on 2010-08-31
12
Medium Priority
?
2,191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

722 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