开发者

How to compare two different dates in dd/mm/yyyy format

Can anyone help me in finding the solution

i just want to compare two dates in dd/mm/yyyy format.

function compareDate(dt1 , dt2 , formatString){var returnVal = 2;
    var dt1Parts;
    var dt2Parts;
    var dt1dd;
    var dt1mm;
    var dt1yyyy;
    var dt2dd;
    var dt2mm;
    var dt2yyyy;
    if(formatString == 'dd/mm/yyyy'){
        dt1Parts = dt1.split('/');
        dt2Parts = dt2.split('/');
        dt1dd = parseInt(dt1Parts[0]);
        dt1mm = parseInt(dt1Parts[1]);
        dt1yyyy = parseInt(dt1Parts[2]);
        dt2dd = parseInt(dt2Parts[0]);
        dt2mm = parseInt(dt2Parts[1]);
        dt2yyyy = parseInt(dt2Parts[2]);
    }
    else if(formatString == 'dd-mm-yyyy'){
        dt1Parts = dt1.split('-');
        dt2Parts = dt2.split('-');
        dt1dd = parseInt(dt1Parts[0]);
        dt1mm = parseInt(dt1Parts[1]);
        dt1yyyy = parseInt(dt1Parts[2]);
        dt2dd = parseInt(dt2Parts[0]);
        dt2mm = parseInt(dt2Parts[1]);
        dt2yyyy = parseInt(dt2Parts[2]);
    }else{
        alert(formatString+' format is not supported.');
    }
    if(dt1yyyy开发者_高级运维 == dt2yyyy && dt1mm == dt2mm && dt1dd == dt2dd){
        returnVal = 0;
    }
    else if(dt1yyyy > dt2yyyy){
        returnVal = 1 ;
    }else if(dt1yyyy == dt2yyyy ){
        if(dt1mm > dt2mm){
            returnVal = 1;
        }else if(dt1mm == dt2mm){
            if(dt1dd > dt2dd){
                returnVal = 1;
            }else{
                returnVal = -1;
            }
        }else{
            returnVal = -1;
        }
    }else{
        returnVal = -1;
    }
    return returnVal;
}

Thanks in advance, Shilpa


Invert the strings to yyyy/mm/dd, or convert them to a number or Date object.

The simplest way just for comparison would be ASCII order. Invert using something like this:


function invert(date) {
 return date.split(/[/-]/).reverse().join("")
}
function compareDates(date1, date2) {
 return invert(date1).localeCompare(invert(date2));
}


Here's how you convert that string format to a date:

var myString = "17/07/1979",
    correctFormat = myString.replace(/(\d+)\/(\d+)\/(\d+)/, "$3/$2/$1"),
    myDate = new Date(correctFormat);


Without knowing what language or class libs you're working with:

Method 1: Resort your strings to be yyyymmdd and the do string compare. Method 2: Stuff yyyy mm and dd into the high, middle, and low bits of an integer and compare.


The easiest way is probably to create 2 javascript Date objects from your input string. You could achieve that by chopping your input into day, month and year. You can use the 'substring' function for that.

Then you can do:

var firstDate = new Date(year1, month1, day1);
var secondDate = new Date(year2, month2, day2);

Once you have 2 date objects, you can use the normal compare operators:

if (firstDate > secondDate)
    // do something
else
    ...


Try this

    var date1=new Date('your date1 string');
    var date2=new Date('your date2 string');

    var difference=new Date(date1.getTime()-date2.getTime());


  if ($.datepicker.parseDate('dd/mm/yy', fDate) > $.datepicker.parseDate('dd/mm/yy', tDate)) {

       //do something                    
}

You can compare two dates.Here I compare from date greater than to date
try this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜