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;
}

1 comment:

Anonymous said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Smartphone, I hope you enjoy. The address is http://smartphone-brasil.blogspot.com. A hug.