Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking date format

Date format

checking date format

by  SeAL  Posted    (Edited  )
Here's my FAQ back! Enjoy ...

function isLeap(year){
if(year % 400 == 0){
return true;
} else if((year % 4 == 0) && (year % 100 != 0)){
return true
} else return false;
};

function days_in(month, year){
if(month == 4 || month == 6 || month == 9 || month == 11){
return 30;
} else if(!isLeap(year) && month == 2){
return 28;
} else if(isLeap(year) && month == 2){
return 29;
} else return 31;
};

function checkDate(myItem){
var myArrayDate, myDay, myMonth, myYear, myString, myYearDigit;
myString = myItem.value + "";
if (myString == "" || myString == "mm/dd/yyyy"){
myItem.value = "mm/dd/yyyy";
return true;
}
myArrayDate = myString.split("/");

myDay = Math.round(parseFloat(myArrayDate[1]));
myMonth = Math.round(parseFloat(myArrayDate[0]));
myYear = Math.round(parseFloat(myArrayDate[2]));
myString = myYear + "";
myYearDigit = myString.length;

if (isNaN(myDay) || isNaN(myMonth) || isNaN(myYear) || (myYear < 1) || (myDay < 1) || (myMonth < 1) || (myMonth > 12) || (myYearDigit != 4) || (myDay > days_in(myMonth, myYear))){
alert("Please check your Date format. (mm/dd/yyyy)");
myItem.value = "mm/dd/yyyy";
return true;
} else{
return false;
}
};


You can easilly change this script to check another date format by changing the split method

Hope this helps ...
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top