2

I have two input fields in which we enter Month-year. And display data in graph representation. Field1:Jan-2016 Field2:Jan-2015

I have to compare and this fields and if Field1>Field2 then alert message has to display.

How to do the comparison in java script

Input type format is Month-year

  • Can you share executable demo/snippet or [JSFiddle](https://jsfiddle.net/) ? – Rayon Apr 03 '16 at 10:01
  • Can compare month using `index` of `months array`..And year using `Number comparison` – Rayon Apr 03 '16 at 10:02
  • Possible duplicate of [Compare two dates with JavaScript](http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Maen Apr 03 '16 at 10:03
  • Could you please share the fiddle with example. I couldn't make itthis format. –  Apr 03 '16 at 10:04

6 Answers6

5

You can use this small sample in your code

var date_1 = new Date('2017', '01');
var date_2 = new Date('2015', '02');

if (date_1.getTime() > date_2.getTime()) {

  /* YOUR CODE */
}

Update: 2016/04/21

With + operator (more laconic):

if (+new Date('2017', '01') > +new Date('2015', '02')) {

  /* YOUR CODE */
}

By using valueOf():

if (new Date('2017', '01').valueOf() > new Date('2015', '02').valueOf()) {

  /* YOUR CODE */
}
Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47
1

You can separate your strings and have a lookup for the month number and return the wanted comparison.

function greater(f1, f2) {
    function getDate(f) {
        var d = f.split('-');
        d[0] = { jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12 }[d[0].toLowerCase()] || 0;
        return d;
    }

    var d1 = getDate(f1),
        d2 = getDate(f2);
    return d1[1] > d2[1] || d1[1] === d2[1] && d1[0] > d2[0];
}

document.write(greater('Jan-2016', 'Jan-2015') + '<br>');
document.write(greater('Feb-2016', 'Jan-2016') + '<br>');
document.write(greater('Feb-2014', 'Jan-2016') + '<br>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can use a JavaScript Date object to compare your dates:

new Date("Jan-2016") > new Date("Jan-2015") //-> true

Bee
  • 88
  • 3
0

Ideally you should compare Date objects, but Date can't parse strings of the format you use.

If you format the date as "2015-01" and "2016-01" you can use Javascript's Date objects to compare them.

This gives you the benefit that you can continue to use these as actual date representations in your script, should you need to.

Note: if you format the date as, ex, "201501" and "201601" you can use simple greater-than or less-than comparisons instead.

So you need to:

  1. Split the date string up into month and year
  2. Convert the month string to an integer
  3. Make a new variable to contain the formatted date
  4. Compare the formatted dates
moopet
  • 6,014
  • 1
  • 29
  • 36
0

You can use a JavaScript library moment.js.

To get the date object, use

var date1= moment("Jan-2016","MMM-YYYY");

Similarly do it for second date and then just compare them as date1 > date2.

Sandeep Sukhija
  • 1,156
  • 16
  • 30
0

If you have the consistent string format of "YYYY-MM", e.g. "2020-10", "2021-01", you don't even have to convert them to date for accurate comparison, just compare the string value directly if you just want to know which month is the later month.

Try this:

var d1 = "2020-12";
var d2 = "2021-01";

var greaterMth = d2 > d1 ? d2 : d1;

alert("Greater month is: " + greaterMth);

But for your case, that will be:

var d1 = new Date("Jan-2016");
var d2 = new Date("Jan-2015");

greaterMth = d2 > d1 ? "Jan-2015" : "Jan-2016";
    
alert("Greater month is: " + greaterMth);

You don't even have to bother about the 1st of the month generated by the new Date().

Antonio Ooi
  • 1,601
  • 1
  • 18
  • 32