Hi,

I'm currently using this fantastic script by Fungus1487 (thanks!) but am having a bit of trouble getting it to detect Saturdays.

For example, when I enter a start date as a Thursday, then enter an end date that's a Friday it'll return 1, fine.

If I enter a date that's a Saturday it'll return 2, when I'd like it to skip the day as it's a weekend.

If I enter a date that's a Sunday it works fine, and skips accordingly, and returns 2.

Everything else is fine though.

Any ideas please? Would really appreciate a bit of help with this, tried alert(odays) to debug it, but that always returns 2 for some reason.

Cheers!

Recommended Answers

All 3 Replies

Simply add 1 with the odays varible!

Math.floor(((((edate.getTime() - sdate.getTime()) / 1000 / 60 / 60 / 24) / 7) * 5) + (odays+1));

Many thanks for the reply, but unfortunately it's still not detecting Saturdays :(

That code by fungus was indeed great. But since he was trying to make reference by days instead of dates, then the output will produce unstable result's.
So its up to you if you want to try this one.
This demo will give you precise result's, counting the days before endDates.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML lang="en">
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>JavaScript Date</TITLE>
<SCRIPT type="text/javascript">
<!--

function countDaysWithoutWeekEnd(startingMonth,startingDate,startingYear,endMonth,endDate,endYear) {

var now = new Date(startingYear,startingMonth,startingDate);
var tillDay = new Date(endYear,endMonth,endDate);

var daysLeft = (tillDay - now); 
daysLeft = ((daysLeft / 1000 / 60 / 60 / 24 / 7) * 5); 
return parseInt(daysLeft);
}

window.onload = function() {

/* Im sure you're familiar on how dates works in javascript.
* 0 specifies the month of (January)
* 1 specifies the month of (February) -- and up to the last month, which makes * 11 for the month of (December).
So dates will be set from: countDaysWithoutWeekEnd(startingMonth,startingDate,startingYear,endMonth,endDate,endYear), which i currently set to "January 31 2009" as the starting reference where days will counted untill "March 3, 2009".
Which gives us a total of (22) days left before "March 3, 2009" less weekend */

alert(countDaysWithoutWeekEnd(0, 31, 2009, 2, 3, 2009)); }
 
//-->
</SCRIPT>
</HEAD>
<BODY>
<P>
<SCRIPT type="text/javascript">
<!--
var today = new Date();

document.write('There are <FONT color="red"><B>' + countDaysWithoutWeekEnd(0,(today.getDate()),2009,2,3,2009) + '</B></FONT> days left before it reaches "March 3 2009" of course less weekend');
//-->
</SCRIPT>
</P>
</BODY>
</HTML>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.