Search This Blog

Friday, July 22, 2011

Date difference in Javascript using asp.net and Calculating Duration Between Two Dates in Years, Months and Days

Use below javascript function to

1) validate the date difference in asp.net
2) difference (number of days, months, years) between two dates.


function validate(ArrivalDate, DepartureDate )
{
var ArrivalDtValue = "15/02/2011"; // ArrivalDate
var DepartureDtValue = "16/02/2011"; // DepartureDate

var currentTime = new Date();
var month = currentTime.getMonth();
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var currentDt = day + "/" + month + "/" + year;

var ArrDt = getDateObject(ArrivalDtValue, "/");
var DeparDt = getDateObject(DepartureDtValue, "/");
var CurDt = new Date();

//Total time for one day
var one_day = 1000 * 60 * 60 * 24;
//Here we need to split the inputed dates to convert them into standard format
//for furter execution
var x = ArrivalDtValue.split("/");
var y = DepartureDtValue.split("/");
//date format(Fullyear,month,date)
var ArrivalDate = new Date(x[2], (x[1] - 1), x[0]);
var DepartureDate = new Date(y[2], (y[1] - 1), y[0]);
var CurrentDate = new Date(year, month, day);
var month1 = x[1] - 1;
var month2 = y[1] - 1;

//Calculate difference between the two dates, and convert to days
var ArrivaldiffDays = Math.abs((CurrentDate.getTime() - ArrivalDate.getTime()) / (one_day));
var DeparturediffDays = Math.abs((CurrentDate.getTime() - DepartureDate.getTime()) / (one_day));


if (ArrDt > DeparDt) {
        alert("Departure date should not be less than Arrival date");
        return false;
}
else if (ArrivalDtValue == DepartureDtValue) {
      alert("Arrival date and Departure date should not be same");
      return false;
}
else if (ArrDt < CurrentDate)
{
      alert("Arrival date should not be less than current date");
      return false;
}
else if (DeparDt < CurrentDate)
{
     alert("Departure date should not be less than current date");
     return false;
}
else
{
     return true;
}
}


function getDateObject(dateString, dateSeperator) {
//This function return a date object after accepting
//a date string ans dateseparator as arguments
var curValue = dateString;
var sepChar = dateSeperator;
var curPos = 0;
var cDate, cMonth, cYear;

//extract day portion
curPos = dateString.indexOf(sepChar);
cDate = dateString.substring(0, curPos);

//extract month portion
endPos = dateString.indexOf(sepChar, curPos + 1);
cMonth = dateString.substring(curPos + 1, endPos);

//extract year portion
curPos = endPos;
endPos = curPos + 5;
cYear = curValue.substring(curPos + 1, endPos);

//Create Date Object
dtObject = new Date(cYear, cMonth - 1, cDate);
//alert(dtObject);
return dtObject;
}





...............................................................................................................
For the date validation (dd/mm/yyyy format) you can refer below site

http://challadotnetfaq.blogspot.com/2011/07/date-validation-using-javascript-in.html



Post a Comment

Popular Posts