/    Sign up×
Community /Pin to ProfileBookmark

calculating workdays

Hi,
How do I calculate 10 or ?? workdays from or before a given date?

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoOct 28.2005 — <i>
</i>function getDateObj(businessDays, year, month, days) {
var now = new Date(year,month,days)
var dayOfTheWeek = now.getDay();
var calendarDays = businessDays;
var Weeks;
var Day = dayOfTheWeek - businessDays;
if (Day &gt;= 6) {
//count the weekend
calendarDays += 2;
//how many whole weeks?
Weeks = Math.floor(businessDays/ 5);
//two days per weekend per week
calendarDays += Weeks * 2;
}
now.setTime(now.getTime() - calendarDays * 24 * 60 * 60 * 1000 - dayOfTheWeek);
return now;
}


That is almost it. I don't have time to test it though, so you will probably have to change some of the numbers in the section checking for the weekends.
Copy linkTweet thisAlerts:
@HarmanauthorOct 28.2005 — Thanks a lot man.
Copy linkTweet thisAlerts:
@HarmanauthorOct 28.2005 — When I try to get the year it gives me 105

var leads = GetDateObj(10, 2005, 11, 11);

alert(leads.getDate() +"-"+ leads.getMonth() +"-"+leads.getYear());

//this gives me


1-11-105
Copy linkTweet thisAlerts:
@konithomimoOct 29.2005 — Sorry . . . the post showed up twice . . .
Copy linkTweet thisAlerts:
@konithomimoOct 29.2005 — The following code works perfectly:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;

function getDateObj(businessDays,date) {
var datelength = date.length;
if (datelength != 8)
{
alert('The date must be entered in as 8 consecutive numbers in the format mmddyyyy');
}

var year = date.charAt(4) + date.charAt(5) + date.charAt(6) + date.charAt(7);
var month = eval ((date.charAt(0) + date.charAt(1))-1);
var day = date.charAt(2) + date.charAt(3);

var y = eval(year);
var m = eval(month);
var d = eval(day);



if ((m &lt; 1)||(y &lt; 1900)||(d &lt; 1)||(d &gt; 31)||(m &gt;12))
{
alert('You have entered in an invalid value for your month, day, or year. The month must be 01-12, the day must be 01-31, and the year must be 1900 or later.');
}

else
{

var weeks = Math.floor(businessDays/5);
var backdays = (businessDays%5);
var total = (weeks*7) + backdays;
var dates = new Date(year,month,day);
document.getElementById('five').value=dates;

var da = document.getElementById('five').value;
var string = da.charAt(0) + da.charAt(1) + da.charAt(2);

if (string == "Sun")
{
total = (total + 2);
}
if (string == "Sat")
{
total = (total + 1);
}

var newdate = new Date(year,month,(day-total));
document.getElementById('five').value=newdate;

da = document.getElementById('five').value;
string = da.charAt(0) + da.charAt(1) + da.charAt(2);

document.getElementById('five').value=newdate;
if ((string == "Sun") || (string =="Sat"))
{
newdate = new Date(year,month,(day-total-2));
}
document.getElementById('five').value=newdate;
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
Number of business days to go back:
&lt;input type="text" name="one" id="one" value="" maxlength="10"&gt;
&lt;br&gt;
Date to go back from(mmddyyyy):
&lt;input type="text" name="two" id="two" value="" maxlength="10" onkeyup="this.value=this.value.replace(/D/,'')" onblur="getDateObj(document.getElementById('one').value,document.getElementById('two').value)"&gt;
&lt;br&gt;
Calculated date:
&lt;input type="text" name="five" id="five" value="" maxlength="10" &gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@konithomimoOct 29.2005 — The above code did not validate the date entered. Below is an all inclusive code that validates the date entered:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;

function getDateObj(businessDays,date,num) {
var datelength = date.length;
if (datelength != 8)
{
alert('The date must be entered in as 8 consecutive numbers in the format mmddyyyy');
}

var year = date.charAt(4) + date.charAt(5) + date.charAt(6) + date.charAt(7);
var month = eval ((date.charAt(0) + date.charAt(1))-1);
var day = date.charAt(2) + date.charAt(3);

var y = eval(year);
var m = month+1;
var d = eval(day);

if (m &lt; 1 || m &gt; 12)
{ // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (d &lt; 1 || d &gt; 31)
{
alert("Day must be between 1 and 31.");
return false;
}
if ((m==4 || m==6 || m==9 || m==11) &amp;&amp; d==31)
{
alert("Month "+m+" doesn't have 31 days!")
return false;
}
if (m == 2)
{
// check for february 29th
var isleap = (y % 4 == 0 &amp;&amp; (y % 100 != 0 || y % 400 == 0));
if (d&gt;29 || (d==29 &amp;&amp; !isleap))
{
alert("February " + y + " doesn't have " + d + " days!");
return false;
}
}

else
{

var weeks = Math.floor(businessDays/5);
var backdays = (businessDays%5);
var total = (weeks*7) + backdays;
var dates = new Date(year,month,day);
document.getElementById('five').value=dates;

var da = document.getElementById('five').value;
var string = da.charAt(0) + da.charAt(1) + da.charAt(2);

if (string == "Sun")
{
total = (total + 2);
}
if (string == "Sat")
{
total = (total + 1);
}

var newdate = new Date(year,month,(day-total));
document.getElementById('five').value=newdate;

da = document.getElementById('five').value;
string = da.charAt(0) + da.charAt(1) + da.charAt(2);

document.getElementById('five').value=newdate;
if ((string == "Sun") || (string =="Sat"))
{
newdate = new Date(year,month,(day-total-2));
}
document.getElementById('five').value=newdate;

total = (total*-1);

newdate = new Date(year,month,(day-total));

document.getElementById('six').value=newdate;
}

}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
Number of business days:
&lt;input type="text" name="one" id="one" value="" maxlength="10"&gt;
&lt;br&gt;
Date to go back from(mmddyyyy):
&lt;input type="text" name="two" id="two" value="" maxlength="10" onkeyup="this.value=this.value.replace(/D/,'')" onblur="getDateObj(document.getElementById('one').value,document.getElementById('two').value, 1)"&gt;
&lt;br&gt;
Calculated date backwards:
&lt;input type="text" name="five" id="five" value="" maxlength="10" &gt;
&lt;br&gt;
Calculated date forward:
&lt;input type="text" name="six" id="six" value="" maxlength="10" &gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@HarmanauthorOct 31.2005 — Thanks dude
Copy linkTweet thisAlerts:
@konithomimoOct 31.2005 — You are very welcome. Let me know if the code works out for you.
Copy linkTweet thisAlerts:
@AncoraNov 01.2005 — I needed something like this too. I don't know, that code posted by konithomimo just not working for me, alerts when it shouldn't. And I don't like putting the date all together with no slashes. I found this code on another forum. Works perfect. Don't remember the dude's name. Maybe others can use it.

[CODE]<HTML>
<Head>
<Script type="text/javascript">

function verify(isField){

var splitDate = isField.value.split("/");
var refDate = new Date(isField.value);
if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))){return false}
return refDate;
}

function calcDay(isForm){

var startDate = verify(isForm.nStart);
var n = 0;
if (startDate)
{
var offset = isForm.nOffset.value;
if (offset > 0)
{
for (i=1; n<offset; i++)
{
var tmp = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()+i);
if (tmp.getDay() != 0 && tmp.getDay() != 6){n++}
}
i--;
}
else {
for (i=1; n>offset; i++)
{
var tmp = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()-i);
if (tmp.getDay() != 0 && tmp.getDay() != 6){n--}
}
i = (i*-1)+1;
}
var resultDate = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()+i);
var slashDate = resultDate.getMonth()+1+"/"+resultDate.getDate()+"/"+resultDate.getFullYear();
isForm.nResult.value = slashDate;
}
if (!startDate)
{
alert('Invalid Date')
isForm.nStart.value = "";
isForm.nStart.focus();
}
}

</Script>
</Head>
<Body>
<br>
<Form name='Form1'>
<Table align='center' cellspacing='0' cellpadding='5' style='font-size:14pt;border:solid black 1px;background-color:lightyellow;'>
<THead><TH colspan='2' style='background-color:lightblue;border-bottom:solid black 1px'>Workday Arithmetic</TH></THead>
<TR><TD align='left'>Reference Date (mm/dd/yyyy):</TD><TD align='right'><input type='text' size='9' name='nStart' onclick="this.value='';this.form.nResult.value=''"></TD></TR>
<TR><TD align='left'>Workdays + or - : </TD><TD align='right'><input type='text' size='9' name='nOffset' onclick="this.value='';this.form.nResult.value=''"></TD></TR>
<TR><TD align='left'>Result Date: </TD><TD align='right'><input type='text' size='9' readonly name='nResult'></TD></TR>
<TR><TD colspan='2' align='center' style='border-top:solid black 1px;background-color:darkorange'><input type='button' value='Calculate' onclick="calcDay(this.form)"></TD></TR>
</Table>
</Form>
</HTML>[/CODE]
×

Success!

Help @Harman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...