Monday, June 17, 2013

Compare two time in javascript

Below is the function used to compare the two time using java scipt.


function checkTime(startTime, endTime) {
//compare two times are same or not....
if (Date.parse('17/06/2013 ' + endTime) - Date.parse('17/06/2013 ' + startTime) == 0) {
        alert('Both time cannot be same');
        return false;
    }
   else if (Date.parse('17/06/2013 ' + endTime) < Date.parse('17/06/2013 ' + startTime)) {
        alert('End time should be greater then Start time');
        return false;
    }
    return true;
}   

This function returns true if end time is greater than the start time. Otherwise it return false.
Instead of  '17/06/201' you can use any date. 


Wednesday, June 12, 2013

Time Validation in Java Script

This function validate the time format in 24 hours. 
function timeValidation(strTime) {
    if (strTime.indexOf(':') == -1 || strTime.length < 4 || strTime.length > 5) {
        return false;
    }
    var n = strTime.split(":");
    if (n.length > 2) {
        return false;
    }

    if (isNaN(n[0]) || n[0] > 23) {
        return false;
    }
    var minutePart = n[1].substring(0, 2);

    if (minutePart.length!=2 || isNaN(minutePart) || minutePart > 59) {
        return false;
    }    
    return true;

}