var onemonth = 3;
var oneyear = 2005;
var twomonth = 10;
var twoyear = 2000;
How can i split this and pare? In this example is:
var firstdate = onemonth + oneyear;
var seconddate = twomonth + twoyear;
if(firstdate < seconddate){
alert('error');
}
How is the best method for pare two date if i have only month and year?
LIVE: /
var onemonth = 3;
var oneyear = 2005;
var twomonth = 10;
var twoyear = 2000;
How can i split this and pare? In this example is:
var firstdate = onemonth + oneyear;
var seconddate = twomonth + twoyear;
if(firstdate < seconddate){
alert('error');
}
How is the best method for pare two date if i have only month and year?
LIVE: http://jsfiddle/26zms/
Share Improve this question asked Feb 21, 2012 at 15:20 Gryz OsheaGryz Oshea 3613 gold badges8 silver badges17 bronze badges 04 Answers
Reset to default 4What about using the native Date object like this?
if (new Date(oneyear, onemonth) < new Date(twoyear, twomonth)){
alert('error');
}else{
alert('ok');
}
With your variables it will yield "ok".
Make then proper dates;
var firstdate = new Date(oneyear, onemonth - 1, 1);
var seconddate = new Date(twoyear, twomonth - 1, 1);
Then the parison is valid (as opposed to paring arbitrarily created integers)
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; // 200503
var twomonth = 10;
if(twomonth < 10)
twomonth = "0" + twomonth ;
var twoyear = 2000;
var twoyearmonth = twoyear + twomonth ; //200010
if(oneyearmonth < twoyearmonth)
{
alert("one month and year leass than tow month and year");
}
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 pare 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?
}