Skip to content Skip to sidebar Skip to footer

Compare Years And Month With Jquery

var onemonth = 3; var oneyear = 2005; var twomonth = 10; var twoyear = 2000; How can i split this and compare? In this example is: var firstdate = onemonth + oneyear; var secondda

Solution 1:

What about using the native Date object like this?

if (newDate(oneyear, onemonth) < newDate(twoyear, twomonth)){
  alert('error');
}else{
  alert('ok');
}

With your variables it will yield "ok".

Solution 2:

Make then proper dates;

var firstdate =  newDate(oneyear, onemonth - 1, 1); 
var seconddate = newDate(twoyear, twomonth - 1, 1); 

Then the comparison is valid (as opposed to comparing arbitrarily created integers)

Solution 3:

acording to me append zero and than concate string

var onemonth = 3; 
if(onemonth < 10)
 onemonth = "0" + onemonth;
var oneyear = 2005; 
var oneyearmonth = oneyear + onemonth; // 200503var twomonth = 10; 
if(twomonth < 10)
 twomonth = "0" + twomonth ;
var twoyear = 2000;
var twoyearmonth = twoyear + twomonth ; //200010if(oneyearmonth < twoyearmonth)
{
   alert("one month and year leass than tow month and year");
}

Solution 4:

There's no need to use the Date object for this case. Simple math is sufficient:

  • Divide the month by twelve.
  • Add this value to the year.
  • Do the same for the other date(s), and compare the values:

Code:

var onemonth = 3;
var oneyear = 2005;
var twomonth = 10;
var twoyear = 2000;

var year1 = oneyear + onemonth / 12;
var year2 = twoyear + twomonth / 12;
if (year1 < year2) {
    // error?
}

Post a Comment for "Compare Years And Month With Jquery"