Wednesday 13 June 2007

checkDate()

This function checks if the date is in a valid format (yyyy-mm-dd hh:mm:ss) and if the date actually exists.


function checkDate(date)
{
var re = /^\d{4}-\d{2}-\d{2}\s{1}\d{2}:\d{2}:\d{2}$/;

var tDate = date.substr(0,10);
var aDate = tDate.split('-');

var tTime = date.substr(11,19);
var aTime = tTime.split(':');

year = aDate[0];
month = aDate[1] - 1;
day = aDate[2];

hour = aTime[0];
min = aTime[1];
sec = aTime[2];

// we check here the basic date values
if ((year < 1980)) {
return false;
}

if (hour > 24) {
return false;
}

if (min > 60) {
return false;
}

if (sec > 60) {
return false;
}

sourceDate = new Date(year,month,day);
if(year != sourceDate.getFullYear())
{
return false;
}

if(month != sourceDate.getMonth())
{
return false;
}

if(day != sourceDate.getDate())
{
return false;
}

if (!re.test(date)) {
return false;
}
return true;
}

Tuesday 12 June 2007

getMonthLength()

I use this function everytime i am working with some type of time handling or validation.


function getMonthLength(month,year) {
var monthlength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
if (month==1 && (year/4==Math.floor(year/4) ||
year/400==Math.floor(year/400)))
{
return 29;
}
else return monthlength[month];
}