1

var date = $("#date").text();
// this date  is variable 01/06/2017


// this  date is also  will be variable
var startdate = 01 / 06 / 2017
if (date == startdate) {}
if (date < startdate) {}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>

how to compare two date and check different condition i got

Sankar
  • 6,908
  • 2
  • 30
  • 53
p.sivakumar
  • 35
  • 2
  • 4
  • What do you mean and want? – Furquan Khan Jun 01 '17 at 08:20
  • var date =Date.parse(_dateTimeHelper.formatLocalDate($("#date").text())); startdate = Date.parse(_dateTimeHelper.formatLocalDate(startdate)); i try this but i got date is 31 may 2017 and startdate is 1 june 2017 if i apply if condition the date is showing greater i should show startdate is greater – p.sivakumar Jun 01 '17 at 08:21
  • Possible duplicate of [How to compare two date values with jQuery](https://stackoverflow.com/questions/3004178/how-to-compare-two-date-values-with-jquery) – Samuil Petrov Jun 01 '17 at 08:22
  • 1
    If you need to manipulate date. I recommend you to use [moment.js](https://momentjs.com) – ndufreche Jun 01 '17 at 08:24
  • This may help as well [How to compare date/time strings](https://stackoverflow.com/questions/6212305/how-can-i-compare-two-time-strings-in-the-format-hhmmss) – a.barbieri Jun 01 '17 at 08:25

3 Answers3

2

You will need to declare both var as Date, now you are just comparing string to string, here i have made an example: https://cg6.tutorialspoint.com/share/661/IytBM5oT

$(document).ready(function(){

$("button").click(function(){

    var date = new Date($("#date").val());

    var startdate = new Date("11/01/2014");

    if(date < startdate)
    {
     $("#datevalueresault").text("startdate is SMALLER than input");
    }
    if(date > startdate)
    {
       $("#datevalueresault").text("startdate is BIGGER than input"); 
    }
    if(date.getTime() === startdate.getTime())
    {
       $("#datevalueresault").text("both date are the SAME"); 
    }
});

});

  • then you need to check the value you are getting from #date ! it needs to be in a format that "date" can read, some thong like: "11/01/2014". – Johann Guðjónsson Jun 01 '17 at 09:12
  • I have changed the answer, just copy my code into your script, and make sure the input is called "date", and create text called "datevalueresault" then you will se the compared resault there, i have also created an example for you: https://cg6.tutorialspoint.com/share/661/IytBM5oT – Johann Guðjónsson Jun 01 '17 at 09:39
1

Maybe this is what you are looking for.

I'm using Date.parse().

var date = Date.parse( $("#date").text() )
var startdate = Date.parse( $("#startdate").text() )

if (date == startdate) {
  $('#comparison').text('same day')
}
if (date < startdate) {
  $('#comparison').text('previous day')
}
if (date > startdate) {
  $('#comparison').text('next day')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>


<p>What is <span id="date">31 may 2017</span> compared to <span id="startdate">1 jun 2017 </span></p>
<p id="comparison"></p>
a.barbieri
  • 2,398
  • 3
  • 30
  • 58
  • mr.a.barbieri i already mention if 31 may 2017 and 1 jun 2017 if use this is condition if (31 may 2017 < 1 jun 2017) it shows 31 may is greater – p.sivakumar Jun 01 '17 at 08:47
  • I updated the code with your dates, but I cannot understand what you mean. Looks good to me... – a.barbieri Jun 01 '17 at 09:06
  • if we use Date.parse this will use if may31 is greater than june 1 – p.sivakumar Jun 01 '17 at 09:08
  • It's very hard to understand what you are trying to say. – a.barbieri Jun 01 '17 at 09:19
  • ...from some other comment you made I might have understood. You need to pass the dates as a `string` inside `Date.parse()` otherwise you are just comparing strings, not dates. In my example I'm taking the strings inside the `span` and pass them into `Date.parse()` usign jQuery. You can do tha way you want though. – a.barbieri Jun 01 '17 at 09:31
0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
 var date =new Date($("#date").text());
 // this date  is variable 01/06/2017


 // this  date is also  will be variable
 var startdate= new Date(2017,5,1)//5 is for sixth month
 if (date==startdate)
 {
 }
 if (date<startdate)
 {
 }
Furquan Khan
  • 1,586
  • 1
  • 15
  • 30