Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Comparing dates without time

Comparing dates without time

Comparing dates without time

How do i compare dates irrespective of time?  For example if I want to see if the dates are equal I cannot do it because when I get the current date it always gives me the time with it.  So if I have a date 04/21/2004 00:00:00 from a form it is not equall to 04/21/2004 04:22:05 that I get from new Date().  How do I strip the date from them and ignore the time?

Thank you in advance


RE: Comparing dates without time

You can build an integer of the fashion YYYYMMDD.  The greater value is the later date.

Here's an example:


var date1 = new Date("4/1/2004");
var year1 = date1.getFullYear();
var month1 = date1.getMonth();
if(month1 < 10)
  month1 = "0" + month1;
var day1 = date1.getDate();
if(day1 < 10)
  day1 = "0" + day1;
var int1 = "" + year1 + month1 + day1;

function compareDates()
 var date2 = new Date();
 var year2 = date2.getFullYear();
 var month2 = date2.getMonth();
 if(month2 < 10)
  month2 = "0" + month2;
 var day2 = date2.getDate();
 if(day2 < 10)
  day2 = "0" + day2;
 var int2 = "" + year2 + month2 + day2;

 if(int1 < int2)
  alert("Fixed date is before today.");
 else if(int2 < int1)
  alert("Fixed date is after today.");
  alert("Fixed date is today.");
}//end compareDates()
fixed date:
document.write((date1.getMonth()+1) + "/" + date1.getDate() + "/" + date1.getFullYear());
<br />
compare date: today.<br />
<input type='button' value='compare' onclick='compareDates()' />

Be careful to use getDate() and not getDay() for the day of the month.  Also notice that if the day (or month) is less than 10, a place-holding zero is added.

'hope this helps.


RE: Comparing dates without time

Something like this should work for you:


    function checkDates(d1, m1, y1, d2, m2, y2) {
        var date1Time = new Date(y1, m1, d1).getTime();
        var date2Time = new Date(y2, m2, d2).getTime();
        if (date1Time < date2Time) {
            alert('Date 1 is earlier than date 2');
        } else if (date1Time > date2Time) {
            alert('Date 1 is later than date 2');
        } else {
            alert('Date 1 and date 2 are the same');

Hope this helps,

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close