Code:
<html>
<head>
<script type='text/javascript'>
//****************************************************************//
// FUNCTION: isDate (dateStr) //
// //
// This function takes a string variable and verifies if it is a //
// valid date or not. Dates must be in the format of dd-mm-yyyy //
// or dd/mm/yyyy. It checks to make sure the month has the proper //
// number of days, based on the month. The function returns true //
// if a valid date, false if not. //
// //
// Day/Month must be 1 or 2 digits, Year must be 2 or 4 digits. //
//****************************************************************//
function isDate(dateStr, method)
{
var datePattern = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/
var matchArray = dateStr.match(datePattern), m = method === 'mm'? 1 : 3, d = m === 1? 3 : 1;
//Check valid format
if (matchArray == null) { return false; }
day = matchArray[d];
month = matchArray[m];
year = matchArray[5];
// check month range
if (month < 1 || month > 12) { return false; }
//Check day range
if (day < 1 || day > 31) { return false; }
//Check months with 30 days
if ((month==4 || month==6 || month==9 || month==11) && day>30) { return false; }
//Check Feb days
if (month == 2)
{
var leapYr = (year%4 == 0 && (year%100 != 0 || year%400 == 0));
if (day > 29 || (day==29 && !leapYr)) { return false; }
}
return true;
}
function getDateObj(dateStr, method)
{
var matchArray = dateStr.split('/'), m = method === 'mm'? 0 : 1, d = !m? 1 : 0;
--matchArray[m];
var dateObj = new Date(matchArray[2], matchArray[m], matchArray[d], 12, 0, 0);
return dateObj;
}
//***************************************************************//
// FUNCTION: dateDiff(Date1Obj, Date2Obj, [units], [precision]) //
// //
// Returns the difference between two date objects in the units //
// specified (optional, default is days). The optional precision //
// parameter determines the number of decimal places the result //
// will be rounded to. Note: When the 'days' units is used and //
// precision is 0, then output will be in calendar days. //
// //
// The units parameter includes the following: d=days (default), //
// h = hours, m = minutes, s = seconds, ms = milliseconds //
//***************************************************************//
function dateDiff(date1Obj, date2Obj, units, precision)
{
//set the default untis
var units = (units)?units:'d';
var calcPrecision = (precision)?Math.pow(10, precision) : 1;
//Calculate the units divisor
switch (units)
{
case 'ms': //Milliseconds
var units = 1;
break;
case 's': //Seconds
var units = 1000;
break;
case 'm': //Minutes
var units = 1000 * 60;
break;
case 'h': //hours
var units = 1000 * 60 * 60;
break;
case 'd': //Calendar Days
default:
var units = 1000 * 60 * 60 * 24;
//Normalize time to 12:00am to count calendar days if precision = 0
if (precision==0)
{
date1Obj.setHours(0);
date2Obj.setHours(0);
}
break;
}
//Convert dates to milliseconds
var date1ms = date1Obj.getTime();
var date2ms = date2Obj.getTime();
//Calculate the difference in selected units
var difference = (date2ms - date1ms) / units;
//Convert to precision parameter
difference = (Math.round(difference*calcPrecision))/calcPrecision;
return difference;
}
function CalculateDiff(date1ID, date2ID)
{
var date1Val = getVal(date1ID);
var date2Val = getVal(date2ID);
var method = document.getElementById('method').checked? 'mm' : 'dd';
if(!isDate(date1Val, method) || !isDate(date2Val, method))
{
setVal('days', '');
alert('Invalid Input Date(s) and/or Invalid Date Format');
return false;
}
setVal('days', dateDiff(getDateObj(date1Val, method), getDateObj(date2Val, method)));
return;
}
function field(id)
{
var ele = document.getElementById( id );
if ( !ele )
{
alert( 'Element not found. id="' + id + '"' );
}
return ele;
}
function setVal(id, val)
{
var ele = field(id);
if (ele)
{
ele.innerHTML = val;
}
}
function getVal(id)
{
var ele = field(id);
var result = null;
if (ele)
{
result = ele.value;
}
return result;
}
</script>
</head>
<body>
<form name='' action=''>
<br>Date 1 <input type='text' value='12/10/2010' id='date1'/>
<br>Date 2 <input type='text' value='14/10/2010' id='date2'/>
<br>Toggle Date Format <input type="checkbox" id="method" value="" onclick="document.getElementById('themethod').innerHTML = this.checked? 'mm/dd/yyyy' : 'dd/mm/yyyy';"> (currently:
<script type="text/javascript">
document.write('<span id="themethod">' + (document.getElementById('method').checked? 'mm/dd/yyyy' : 'dd/mm/yyyy') + '<\/span>');
</script>)
<br>
<input type='button' value='Calculate difference' onclick='CalculateDiff("date1","date2");'>
<br> Calculated Difference
<table border='1'>
<tr>
<th>Days </th><td><textarea name='days' rows='1' cols='8' id='days'> </textarea></td>
</tr>
</table>
</form>
</body>
</html>
Bookmarks