0

I am comparing two date, one is the current date and the other is selected from the input date type. I want if the selected date is less than the current date return true, but it does not returning true;

    function check_start_date(obj){


          var value= obj.value;
          var curr_date= new Date();
          var selected_date= new Date(value);
          curr_date= trim_time(curr_date);
          if(selected_date < curr_date){
               alert("less");
               return true;
           }
          else{
              return  false;
              }


    }
4
  • hard to say what's wrong without the actual values Commented Sep 14, 2014 at 12:58
  • What does curr_date= trim_time(curr_date); do with the value? Commented Sep 14, 2014 at 13:01
  • @soulcheck 09/02/2014 It alerts true, if i select 09/15/2014 it return false. but when i select 09/14/2014(current date ) it again alert less . Commented Sep 14, 2014 at 13:03
  • Dear @Pengtuzi function trim_time(obj){ obj.setFullYear(obj.getFullYear(),obj.getMonth(),obj.getDate()); obj.setHours(0); obj.setMinutes(0); obj.setSeconds(0); return obj; } Commented Sep 14, 2014 at 13:04

4 Answers 4

2

Your trim_time function is not removing milliseconds, so the trimmed value curr_date will be slightly greater than selected_date, except when the current time falls exactly on the second (milliseconds is 0).

Sign up to request clarification or add additional context in comments.

Comments

1

I Changed My trim_time(obj) function and it did work :)

 function trim_time(obj){
     var obj1= new Date(obj.getFullYear(),obj.getMonth(),obj.getDate(),0,0,0);
    return obj1;
    }

Comments

0

My solution would be to use the Number() function. Then compare the two resulting numbers.

var curr_date = Number(new Date());

...

var selected_date = Number(new Date(value));

...

Then compare etc. Not tested btw, just some thoughts.

MDN explanation: Number() Function

2 Comments

You don't need to convert to numbers to be able to compare dates; operators like < > and == work fine on Date objects themselves. Also, if you did want raw numbers from the date objects for some reason, you could just use the getTime() function.
I don't disagree with you :), just something I would try if I was having problems with comparing dates.
0

You need to convert the dates to numbers (for simple comparison). My solution would be:

function check_start_date( obj ) {
  var value = new Date( obj.value ) * 1; //--- convert to number
  var curr_date = new Date();
  curr_date = new Date( curr_date.getFullYear(), curr_date.getMonth(), curr_date.getDate()) * 1; //--- remove time from the date
  if( value < curr_ date ) {
    alert( 'less' );
    return true;
  } else {
    return false;
  }
}

Comments

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.