/    Sign up×
Community /Pin to ProfileBookmark

difference date in days

Hello,
i have this date 01/12/2004.
How I make to gain the difference between two dates in days?
and to color the background of 3 different cells to second that they are passes 30days 40days or +?

thanks
Manu

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 12.2005 — compare the dates using new Date() constructor

see an example:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function calculate(f){
var entry = f.elements['newD'].value.split('/');//splits input value;
if((entry.length<3)||(entry[0].length!=2)||(entry[1].length!=2)||(entry[2].length!=4)||(Number(entry[0]>31))||(Number(entry[1]>12))){//some limitative conditions
alert('Plese input data in format dd/mm/yyyy !');
f.difD.value = '';
return false;
}
y=entry[2]*1;
m=entry[1]*1-1;//sets months to default values i.e Jan = 0; Feb = 1; etc.
d=entry[0]*1;
var newDate = new Date(y,m,d);//sets the new date
var today = new Date();//sets the today date
dif = newDate-today;//difference in milliseconds
dif = Math.ceil(dif/1000/60/60/24);//difference in days
f.difD.value = dif + ' days';//assign the difference
}
</script>
</head>
<body>
<form>
<input name="newD" type="text">
Enter the new date in format dd/mm/yyyy<br>
<input type="button" onclick="calculate(this.form)" value="Calculate">
<br>
<input name="difD" type="text">
difference in days (new date - today)
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@doppioauthorJan 12.2005 — ok thanks
Copy linkTweet thisAlerts:
@Warren86Jan 12.2005 — <HTML>

<Head>

<Script Language=JavaScript>

var refDate = 0;
var currField = "";

function calcJulian(isDate){

isValid = true;
tmp = isDate.split("/");
if (tmp[0] <1 || tmp[0] >12 || tmp[1] <1 || tmp[1] >31 || tmp[2] < 1970){isValid = false};
if ((tmp[0] == 4 || tmp[0] == 6 || tmp[0] == 9 || tmp[0] == 11) && tmp[1] > 30){isValid = false}
if ((tmp[0] == 1 || tmp[0] == 3 || tmp[0] == 5 || tmp[0] == 7 || tmp[0] == 8 || tmp[0] == 10 || tmp[0] == 12) && tmp[1] > 31){isValid = false}
isLeap = tmp[2]%4;
if ((tmp[0] == 2 && isLeap == 0) && tmp[1] > 29){isValid = false}
if ((tmp[0] == 2 && isLeap != 0) && tmp[1] > 28){isValid = false}
if (isNaN(Date.parse(isDate)) || !isValid){alert("Invalid Date"); return false}
else {
gregDate = new Date(isDate);
year = gregDate.getFullYear();
month = gregDate.getMonth()+1;
day = gregDate.getDate();
A = Math.floor((7*(year+Math.floor((month+9)/12)))/4);
B = day+Math.floor((275*month)/9)
isJulian = (367*year)-A+B+1721014;
return isJulian
}
}

function calcDiff(isDate){

testStr = isDate;
testDate = /[0-9]{1,2}[/][0-9]{1,2}[/][0-9]{4}/.test(testStr);
if (testDate)
{
if (calcJulian(testStr))
{
calcDate = calcJulian(testStr)
diff = refDate-calcDate;
if (diff > 30 && diff < 41){currField = document.getElementById('n30');currField.style.backgroundColor='darkgoldenrod'}
if (diff > 40 && diff < 61){currField = document.getElementById('n40');currField.style.backgroundColor='darkgoldenrod'}
if (diff > 60){currField = document.getElementById('nX');currField.style.backgroundColor='darkgoldenrod'}
}
}

}

function getToday(){

gregDate = new Date();
year = gregDate.getFullYear();
month = gregDate.getMonth()+1;
day = gregDate.getDate();
A = Math.floor((7*(year+Math.floor((month+9)/12)))/4);
B = day+Math.floor((275*month)/9)
isJulian = (367*year)-A+B+1721014;
refDate = isJulian;
document.getElementById('subjDate').focus();
}

function clearIt(isField){

isField.value = "";
isField.focus();
if(currField != ""){currField.style.backgroundColor='lightyellow'}
}

window.onload=getToday;


</Script>

</Head>

<Body>

<br>

<Form name='Form1'>

<Table align='center' width='300' cellpadding='5' cellspacing='0' style='font-size:14pt;border:solid black 1px'>

<TR bgcolor='lightblue'><TD>Test Date: </TD><TD colspan='2'><input type=text size=9 name='subjDate' onkeyup="calcDiff(this.value)" onclick="clearIt(this)"></TD</TR>

<TR bgcolor='lightyellow'><TD id='n30' width='33%' align='center' style='border-right:solid black 1px;border-top:solid black 1px'> +30 days </TD><TD id='n40' width='33%' align='center' style='border-right:solid black 1px;border-top:solid black 1px'> +40 Days </TD><TD id='nX' width='33%' align='center' style='border-top:solid black 1px'> +60 days</TD></TR>

</Table>

</Form>

</Body>

</HTML>
×

Success!

Help @doppio 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,
)...