Date Compare

[Total: 0    Average: 0/5]

This shows how to compare 2 dates, and determine if they are more than 2 years apart

Body Code:

<script type=text/javascript>
var _m = "3"
var _d = "18"
var _y1 = "2001"
var _y2 = "2020"
var _d1 = new Date()
    _d1.setMonth(_m)
	_d1.setDate(_d)
	_d1.setYear(_y1)
var _d2 = new Date()
    _d2.setMonth(_m)
	_d2.setDate(_d)
	_d2.setYear(_y2)
function DaysDelta(_v1,_v2) {
  var _delta = 0;
  var _a1 = _v1;
  var _a2 = _v2;
  if (_v1.getTime()<_v2.getTime()) {
    _a1 = _v2;
	_a2 = _v1;
  }
  _delta = _a1.getTime() - _a2.getTime(); // in msecs
  _delta /= (1000 * 60 * 60 * 24); // days
  return _delta;
}
document.write("Date 1 = "+_m+"/"+_d+"/"+_y1+"<BR>")
document.write("Date 2 = "+_m+"/"+_d+"/"+_y2+"<BR>")
if (DaysDelta(_d1,_d2) > ( 2 * 365)) {
  document.write("Delta is greater than 2 years!");
}
else {
  document.write("Delta is not greater than 2 years!");
}
</script>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *