0

I have two dates in format 14-May-2013 and 15-Jun-2013

How can I compare this two dates?

This is the code I have, but it doesn't seem to be working fine.

if (col == 8 && m == 2) {
    var firstValue = document.getElementById(tableID+m+""+6).innerHTML.split('-');
    var secondValue = document.getElementById(tableID+m+""+7).innerHTML.split('-');

    var firstDate=new Date();
    firstDate.setFullYear(firstValue[0],firstValue[1], firstValue[2]);

    var secondDate=new Date();
    secondDate.setFullYear(secondValue[0],secondValue[1], secondValue[2]);     

    if (firstDate < secondDate) 
    {
        alert("First Date  is less than Second Date");
    }
    else
    {
        alert("Second Date  is less than First Date");
    }   
}

What am I doing wrong and how do I make it work right?

Thanks!

1
  • Why don't you transform these date into milliseconds? Commented Jun 10, 2013 at 14:37

3 Answers 3

2

format 14-May-2013 and 15-Jun-2013

firstDate.setFullYear(firstValue[0],firstValue[1], firstValue[2]);

What am I doing wrong?

setFullYear does not take month names or abbreviations. You will have to parse your date format to get the month number first.

function parse(str) {
    var vals = str.split('-');
    return new Date(
        +vals[0],
        ["jan","feb","mar","apr","may","jun","jul","aug","sep",
         "oct","nov","dez"].indexOf(vals[1].toLowerCase()),
        +vals[2]
    );
}
var firstDate = parse(document.getElementById(tableID+m+""+6).innerHTML)
var secondDate = parse(document.getElementById(tableID+m+""+7).innerHTML);
if (firstDate < secondDate) {
    alert("First Date  is less than Second Date");
} else {
    alert("Second Date  is less than or equal to First Date");
}
Sign up to request clarification or add additional context in comments.

Comments

1

Change to this

if(firstDate.getTime() < secondDate.getTime()){
  //do something
} else {
  //do something else
}

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC

So you are basically comparing two Millisecond representations of the date.

1 Comment

Why? The < comparison operator does already convert them into numbers.
1

When I compare two I transform the time into Unix time. The results are much more reliable. In javascript this would be the getTime() function.

So for you firstDate.getTime() < secondDate.getTime().

http://www.tutorialspoint.com/javascript/date_gettime.htm

4 Comments

Why? The < comparison operator does already convert them into numbers.
If you don't use unix time, its just harder/more work to compare two dates. Is May (05), 31st greater than or less than June (06), 20th? Javascript might say 20 is less than 31 or it might say 05 is less than 06. This would take a more detailed function. Its much easier to work with dates that are just one numeric constant (as opposed to dates that are 3 or more like June 10, 2013).
Why would you stringify the date objects to May (05), 31st (assuming you have them parsed already)? What I said is that the < comparison operator implicitly converts the objects into unix (ms) timestamps before comparing them - and you don't need to call getTime().
Bergi, come to think about it, I believe you're right. Comparison operators in javascript do convert dates to primitive values i.e. unix time. I've always used getTime() to compare javascript date objects, and of course, receive results as expected. I'm curious as to why, then, the getTime() function solved OP's situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.