0

How can I check that a date is greater than or less than the specified date using javascript by passing the month and year only in MMM-YYYY Format.

For example :

Let the user selects JUL from month dropdown and 2016 from years dropdown ,

Now How can I check that the selected values are greater than / less than JUN 2016 .

I've taken the input in Session variables as session("month") , session("yrs").

I've tried the following but nothing happens :

var d1 = new Date('JUN-2016')
var d2 = new Date(<% =session("month")+"-"+session("yrs") %>) )
if(d2.getTime() > d2.getTime())
{
alert('Greater than jun 2016');
}
else
{
alert('Less than jun 2016');
}

I've gone through this link Compare two dates with JavaScript but didn't find the solution for my condition.

As this application is developed on VB so i don't have much knowledge about that.

How can I resolve this .

Please suggest

Thanks in advance.

Community
  • 1
  • 1
Alina Anjum
  • 1,178
  • 6
  • 30
  • 53

4 Answers4

0

Try this -

var x = new Date('JUN-2016');
var y = new Date('MAY-2016');
console.log(+x < +y);
console.log(+x > +y);
console.log(+x === +y);
Rohan Veer
  • 1,374
  • 14
  • 21
  • I've tried this : var d1 = new Date('JUN-2016'); var d2 = new Date(<% =session("month")+'-'+session("yrs") %>) ); if(d2.getTime() > d2.getTime()) { alert('greater than jun 2016'); } but there is a sytax error where I am concatenating the month and year , can u please help me – Alina Anjum Jul 13 '16 at 07:07
  • Why are you comparing same date - if(d2.getTime() > d2.getTime()) – Rohan Veer Jul 13 '16 at 07:32
  • Thanks Rohan I modified your code , it worked for me thanks – Alina Anjum Jul 13 '16 at 07:45
0

You just have to do a simple compare of dates. Please read more about "Date" here: http://www.w3schools.com/js/js_date_methods.asp

Maybe this code might help:

var day = 1; // Since you don't care about the day just use the first day
var month = session("month"); // Use the number of the month instead of the name
var year = session("yrs");
var dateJune = Date.parse("2016-07-01"); // June
var dateInput = Date.parse(year + "-" + month + "-" + day);
// Compare dates
if (dateJune > dateInput) {
    // The input is before june
} else if (dateJune < dateInput) {
    // The input is after june
} else {
    // It is june
}

You need a valid date format to be able to parse the date. Instead of getting "JUN" from your select box it would be better to get the number of the month instead. A select box should look like this:

<select>
<option value="01">Jan</option>
<option value="02">Feb</option>
...
</select>

If this is not an option you can use a function that calculates the number for you if you know then string values that your month variable might have:

function (nameOfMonth) {
   switch (nameOfMonth) {
     case 'Jan':
        return "01";
     case 'Feb':
        return "02";
     ....
   }
}
Kamelkent
  • 329
  • 7
  • 16
  • How the input the date in Mon-yyyy format . i've edited my question and added the code , please help me :( – Alina Anjum Jul 13 '16 at 07:24
  • @AlinaAnjum I have edited my answer. What you need is a valid date format to be able to parse, otherwise it won't work. I hope this helps. – Kamelkent Jul 13 '16 at 07:38
0

First convert it to date object

  // convert to a parseable date string:
   var dateStrA = "28/12/2013 16:20:22".replace( /(\d{2})\/(\d{2})\/(\d{4})/,        "$2/$1/$3");
    var dateStrB = "28/12/2013 16:20:11".replace( /(\d{2})\/(\d{2})\/(\d{4})/,        "$2/$1/$3");

     // now you can compare them using:
      new Date(dateStrA) > new Date(dateStrB);
Jayanti Lal
  • 1,175
  • 6
  • 18
0
var date1 = Math.ceil(Math.abs(first date) / (1000 * 3600 * 24));
var date2 = Math.ceil(Math.abs(second date) / (1000 * 3600 * 24));
if(date1  > date2 )
   alert("date1");
else
   alert("date2");
Ram Segev
  • 2,563
  • 2
  • 12
  • 24
  • How the input the date in Mon-yyyy format . i've edited my question and added the code , please help me :( – Alina Anjum Jul 13 '16 at 07:24
  • try this function getFormattedDate(input) { var pattern = /(.*?)\/(.*?)$/; var result = input.replace(pattern,function(match,p1,p2){ var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; return (p2<10?"0"+p2:p2) + " " + months[(p1-1)]; }); alert(result); } getFormattedDate("1/2013"); – Ram Segev Jul 13 '16 at 07:59