/    Sign up×
Community /Pin to ProfileBookmark

difference between two working dates (UK Date)

Hi, someone very kindly posted some excellent code a few months ago which calculates the number of working days (Monday through to friday) between two inputted dates. Unfortunately it is for US dates and my javascript is not good enough to change the original code for UK date format (e.g. dd/mm/yyyy)

i have searched the web for many hours to no avail …. please could somebody help me to change this code so that it converts uk dates ..

[CODE]<HTML>
<Head>
<Script Language=JavaScript>

var nDay = new Array();
var i = 0;

function verify(isField){

splitDate = isField.value.split(“/”);
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}
nDay[i++] = refDate.getDay();
return refDate;
}

function countDays(isForm){

isValid = true;
startDate = verify(isForm.nStart);
if (isValid){endDate = verify(isForm.nEnd)} else {endDate = false}
if (startDate && endDate)
{
daysApart = Math.round(((endDate-startDate)/86400000));
if (nDay[0] == 0 || nDay[0] == 6)
{isValid = false; startDate=false}
if (daysApart <= 0){isValid = false;endDate=false}
if (isValid)
{
workDays = daysApart-(parseInt(daysApart/7)*2);
if (daysApart < 7 && nDay[1] != 0 && nDay[0]-nDay[1] >= 1){workDays = workDays-2}
if (daysApart < 7 && nDay[1] == 0){workDays–}
isForm.nWork.value = workDays;
i = 0;
}
}
if (!startDate)
{
alert(‘Invalid Start Date.nStart Date must be a weekday.nStart Date must be earlier than End Date.’)
isForm.nStart.value = “”;
isForm.nStart.focus();
i = 0;
}
else if (!endDate)
{
alert(‘Invalid End Date.nEnd Date must be later than Start Date.’)
isForm.nEnd.value = “”;
isForm.nEnd.focus();
i = 0;
}
}

</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;width:250px’>
<THead><TH colspan=’2′ style=’background-color:lightblue;border-bottom:solid black 1px’>Workdays Elapsed</TH></THead>
<TR><TD align=’left’>Start Date: </TD><TD align=’right’><input type=text size=9 name=’nStart’ onclick=”this.value=”;this.form.nWork.value=””></TD></TR>
<TR><TD align=’left’>End Date: </TD><TD align=’right’><input type=text size=9 name=’nEnd’ onclick=”this.value=”;this.form.nWork.value=””></TD></TR>
<TR><TD align=’left’>Workdays: </TD><TD align=’right’><input type=text size=9 readonly name=’nWork’></TD></TR>
<TR><TD colspan=’2′ align=’center’ style=’border-top:solid black 1px;background-color:darkorange’><input type=button value=’Calculate’ onclick=”countDays(this.form)”></TD></TR>
</Table>
</Form>
</HTML>[/CODE]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guySep 08.2005 — what if you just change this line?
if (splitDate[1] &lt; 1 || splitDate[1] &gt; 12 || refDate.getDate() != splitDate[0] || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))){return false}

what happens then
Copy linkTweet thisAlerts:
@arobbo61authorSep 08.2005 — thats what i thought , i changed that and it didn't seem to do anything , which confused me greatly ....
Copy linkTweet thisAlerts:
@arobbo61authorSep 08.2005 — but maybe thats because the validator function needs to be changed as well ....
Copy linkTweet thisAlerts:
@MjhLkwdSep 08.2005 — This works.

[CODE]
<HTML>
<Head>
<Script Language=JavaScript>

var nDay = new Array();
var i = 0;

function verify(isField){

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

function countDays(isForm){

isValid = true;
startDate = verify(isForm.nStart);
if (isValid){endDate = verify(isForm.nEnd)} else {endDate = false}
if (startDate && endDate)
{
daysApart = Math.round(((endDate-startDate)/86400000));
if (nDay[0] == 0 || nDay[0] == 6)
{isValid = false; startDate=false}
if (daysApart <= 0){isValid = false;endDate=false}
if (isValid)
{
workDays = daysApart-(parseInt(daysApart/7)*2);
if (daysApart < 7 && nDay[1] != 0 && nDay[0]-nDay[1] >= 1){workDays = workDays-2}
if (daysApart < 7 && nDay[1] == 0){workDays--}
isForm.nWork.value = workDays;
i = 0;
}
}
if (!startDate)
{
alert('Invalid Start Date.nStart Date must be a weekday.nStart Date must be earlier than End Date.')
isForm.nStart.value = "";
isForm.nStart.focus();
i = 0;
}
else if (!endDate)
{
alert('Invalid End Date.nEnd Date must be later than Start Date.')
isForm.nEnd.value = "";
isForm.nEnd.focus();
i = 0;
}
}

</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;width:250px'>
<THead><TH colspan='2' style='background-color:lightblue;border-bottom:solid black 1px'>Workdays Elapsed</TH></THead>
<TR><TD align='left'>Start Date: </TD><TD align='right'><input type=text size=9 name='nStart' onclick="this.value='';this.form.nWork.value=''"></TD></TR>
<TR><TD align='left'>End Date: </TD><TD align='right'><input type=text size=9 name='nEnd' onclick="this.value='';this.form.nWork.value=''"></TD></TR>
<TR><TD align='left'>Workdays: </TD><TD align='right'><input type=text size=9 readonly name='nWork'></TD></TR>
<TR><TD colspan='2' align='center' style='border-top:solid black 1px;background-color:darkorange'><input type=button value='Calculate' onclick="countDays(this.form)"></TD></TR>
</Table>
</Form>
</HTML>[/CODE]
Copy linkTweet thisAlerts:
@arobbo61authorSep 08.2005 — your an absolute star !!!!!

where was i going wrong ?
Copy linkTweet thisAlerts:
@bathurst_guySep 08.2005 — Well that was a challenge, there is probably a better or easier way but i dont know it
&lt;HTML&gt;
&lt;Head&gt;
&lt;Script Language=JavaScript&gt;

var nDay = new Array();
var i = 0;

function verify(isField){

splitDate = isField.value.split("/");

// Change the month area to days
// Change the day area to months
// Due to american date format being stupid
var dDay = splitDate[0];
var dMonth = splitDate[1];

refDate = new Date(dMonth+"/"+dDay+"/"+splitDate[2]);

if (dMonth &lt; 1 || dMonth &gt; 12 || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))){return false}
nDay[i++] = dDay;
//alert("day = "+dDay+" month = "+dMonth);
//alert(refDate);
return refDate;
}

function countDays(isForm){

isValid = true;
startDate = verify(isForm.nStart);
if (isValid){endDate = verify(isForm.nEnd)} else {endDate = false}
if (startDate &amp;&amp; endDate)
{
daysApart = Math.round(((endDate-startDate)/86400000));
if (nDay[0] == 0 || nDay[0] == 6)
{isValid = false; startDate=false}
if (daysApart &lt;= 0){isValid = false;endDate=false}
if (isValid)
{
workDays = daysApart-(parseInt(daysApart/7)*2);
if (daysApart &lt; 7 &amp;&amp; nDay[1] != 0 &amp;&amp; nDay[0]-nDay[1] &gt;= 1){workDays = workDays-2}
if (daysApart &lt; 7 &amp;&amp; nDay[1] == 0){workDays--}
isForm.nWork.value = workDays;
i = 0;
}
}
if (!startDate)
{
alert('Invalid Start Date.nStart Date must be a weekday.nStart Date must be earlier than End Date.')
isForm.nStart.value = "";
isForm.nStart.focus();
i = 0;
}
else if (!endDate)
{
alert('Invalid End Date.nEnd Date must be later than Start Date.')
isForm.nEnd.value = "";
isForm.nEnd.focus();
i = 0;
}
}

&lt;/Script&gt;
&lt;/Head&gt;
&lt;Body&gt;
&lt;br&gt;
&lt;Form name='Form1'&gt;
&lt;Table align='center' cellspacing='0' cellpadding='5' style='font-size:14pt;border:solid black 1px;background-color:lightyellow;width:250px'&gt;
&lt;THead&gt;&lt;TH colspan='2' style='background-color:lightblue;border-bottom:solid black 1px'&gt;Workdays Elapsed&lt;/TH&gt;&lt;/THead&gt;
&lt;TR&gt;&lt;TD align='left'&gt;Start Date: &lt;/TD&gt;&lt;TD align='right'&gt;&lt;input type=text size=9 name='nStart' onclick="this.value='';this.form.nWork.value=''"&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD align='left'&gt;End Date: &lt;/TD&gt;&lt;TD align='right'&gt;&lt;input type=text size=9 name='nEnd' onclick="this.value='';this.form.nWork.value=''"&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD align='left'&gt;Workdays: &lt;/TD&gt;&lt;TD align='right'&gt;&lt;input type=text size=9 readonly name='nWork'&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD colspan='2' align='center' style='border-top:solid black 1px;background-color:darkorange'&gt;&lt;input type=button value='Calculate' onclick="countDays(this.form)"&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;/Table&gt;
&lt;/Form&gt;
&lt;/HTML&gt;
Copy linkTweet thisAlerts:
@arobbo61authorSep 08.2005 — your both stars !!!

thank you very much for your help ....

much appreciated
×

Success!

Help @arobbo61 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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