6

I have two date in which one is dd-mm-yyyy hh:mm format and another in dd-mm-yyyy (D1) format fristly i split the dd-mm-yyyy hh:mm format date to get dd-mm-yyyy (D2) format only then i compare the date D2 and D1 like

var D1 = new Date(); 
var D2 = new Date(); 
// D1 = 03-05-2014  this date take as an example
// D2 = 28-04-2014 00:00  this date take as an example
// D1 and D2 are taken by input fields.
    split the D2 date

dat = D2.split(' ');
D2 = dat[0];
//finally D2 is 28-04-2014
if(D2<=D1)
{
  echo "ok";
}
else{
  echo "something is wrong";
}

I am always getting the else part, is this because i split the date from 28-04-2014 00:00 to 28-04-2014 ?

6
  • what type of result you need as the output ? Commented May 3, 2014 at 10:40
  • true part of if condition Commented May 3, 2014 at 10:41
  • actually it compare only day i don't know why, it don't compare whole date Commented May 3, 2014 at 10:46
  • See stackoverflow.com/a/23402449/1470607 Commented May 3, 2014 at 10:52
  • not getting an answere.. Commented May 3, 2014 at 11:02

7 Answers 7

7
dateFirst = D1.split('-');
dateSecond = D2.split('-');
var value = new Date(dateFirst[2], dateFirst[1], dateFirst[0]); //Year, Month, Date
var current = new Date(dateSecond[2], dateSecond[1], dateSecond[0]);

than use the if condition

if(D2<=D1)
{
console.log('ok');
}
else
{
console.log('something is wrong');
}
Sign up to request clarification or add additional context in comments.

Comments

5

This is you need actually

var D1 = "03-05-2014";
var D2 = "28-04-2014 00:00";

if ((new Date(D1).getTime()) >= (new Date(D2).getTime())) {
    alert('correct');
} else {
    alert('wrong');
}

WORKING DEMO

3 Comments

by this i have no need to split date to get 28-04-2014 from 28-04-2014 00:00 ?
yes.parse any date string to object and compare times
You don't need to getTime because of how valueOf a Date is defined
3

That is a string comparison you are doing, not a date comparison

In terms of strings... 2 is greater than 0... That's why you always hit the "something is wrong" statement

EDIT: This is an explanation of what went wrong // Creates a date variable var D1 = new Date();

// Creates another date variable
var D2 = new Date();

// Converts this date variable into a string
D1 = 03-05-2014 

// This is too is converted into a string
D2 = 28-04-2014 00:00

    dat = D2.split(' ');
D2 = dat[0];

//finally D2 is 28-04-2014  <-- true, but it is a string
if(D2<=D1){    //   <-- At this point you are doing a string comparison

        echo "ok";

} else {

    echo "something is wrong";

}

EDIT: This is a possible solution. . .

Instead of that, do this

var D1 = new Date();
var D2 = new Date();

if(D2.getTime() <= D1.getTime()){
    echo "ok";
} else {
    echo "something is wrong";
}

EDIT: If you are getting it from an input field, then do this

// Make sure you dates are in the format YYYY-MM-DD.
    // In other words, 28-04-2014 00:00 becomes 2014-04-28 00:00
// Javascript Date only accepts the ISO format by default
var D1 = new Date( $('#input1#').val() );
var D2 = new Date( $('#input2#').val() );

    if(D2.getTime() <= D1.getTime()){
    echo "ok";
} else {
    echo "something is wrong";
}

4 Comments

this has to be a comment
i take variable date type
Tarzan, you did make it a date at the beggining....but later you changed it into a string using D1 = 03-05-2014. Look at my answer for an explanation and solution.
no no i am taking date from input field, here i mention only to take as an example
0

Try:

var msg = new Date("03-05-2014").getTime() >= new Date("28-04-2014 00:00").getTime() ? 'Correct' : 'Wrong';
alert(msg);

Comments

0
var now = new Date();
var end = new Date(//some other date hear);
if(now>end)
{
     console.log('now=',now);
}
else
{
    console.log('end=',end);
}

Comments

0

You can date compare as most simple and understandable way like.

<input type="date" id="getdate1" />   //with time include
<input type="date" id="getdate2" /> // without time include

so firstly write a common method to parse date method.

    <script type="text/javascript">
                function parseDate(input) {   
                  var parts = input.split(' ');
              var datecomp=parts[0];  // will get 
              var timecomp=parts[1];  // will get time
                  var dparts=datecomp.split('-');
             if(timecomp!=undefined && timecomp!=null){ //if there is like
                             var tparts=timecomp.split(':');
     return new Date(dparts[2], dparts[1]-1, dparts[0], tparts[0], tparts[1]); 


 }else{  // for when no time is givin
    return new Date(dparts[2], dparts[1]-1, dparts[0]); 
                                              }
                                             return "";
                                              }
            </script>

by here you can call above method

<script type="text/javascript">

              $(document).ready(function(){
              //parseDate(pass in this method date);
                    Var Date1=parseDate($("#getdate1").val());
                        Var Date2=parseDate($("#getdate2").val());
               //use any oe < or > or = as per ur requirment 
               if(Date1 = Date2){
         return false;  //or your code {}
}
 });
    </script>

Just use parseDate method and compare any type of the date.

Comments

-1

Convert them into milliseconds, then do comparision

2 Comments

How would you do that?
This should be a comment. If you want to, you could turn it into a full answer by adding more detailed information and/or examples.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.