Friday, November 19, 2010

How to check the given date is before/after the specified date using javascript

How to check if  the given date is before/after the specified date using javascript

<script language="javascript">

// Checks if this date is before the specified date
Date.prototype.before =
function(_date)
{
if
(this.getFullYear() > _date.getFullYear())
{
return false
;
}
else if
(this.getFullYear() == _date.getFullYear())
{
if
(this.getMonth() > _date.getMonth())
{
return false
;
}
else if
(this.getMonth() == _date.getMonth())
{
if
(this.getDate() > _date.getDate())
{
return false
;
}
}
}
return true
;
}
;
// Checks if this date is after the specified date
Date.prototype.after =
function(_date)
{
if
(this.getFullYear() < _date.getFullYear())
{
return false
;
}
else if
(this.getFullYear() == _date.getFullYear())
{
if
(this.getMonth() < _date.getMonth())
{
return false
;
}
else if
(this.getMonth() == _date.getMonth())
{
if
(this.getDate() < _date.getDate())
{
return false
;
}
}
}
return true
;
}

</script>

Usage :

.....


date t1 = new Date();
date t2 = new Date(yyyy, mm, dd);

alert(t1.after(t2);

....

1 comment: