Solved

How to exclude weekends and go previous working day in javascript

Posted on 2010-08-31
12
1,662 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now