[Javascript] Form field time and date check

Walter Torres walter at torres.ws
Tue Jun 18 11:04:27 CDT 2002


Below is what I use for Date and Time checking...

Hope this helps

Walter

====================================
function isDate (year, month, day)
{
	// Original post: Dan Osborne <dano at specialist.co.uk>
	//                4/26/1999
	//                JavaScript <javascript at LaTech.edu>
	// Modified by:   Walter Torres <walter at torres.ws>
	//                4/29/2001

	// This is an interesting way to do validation of dates!

	// First, decrement the given month;
	// since humans think in base ONE and
	// JavaScript dates for months think in base ZERO.
	//      Human month range : 1 - 12
	// JavaScript month range : 0 - 11
	month--;  

	// Then, create a new JS Date Object based upon given values.
	// NOTICE: This will convert 2/32/2000 to 3/3/2000
	//         JavaScript 'helps' you like this.
	var objTempDate = new Date(year,month,day);

	// Now, tear that new Data Object apart and see if the
	// values it gives back matches what we gave it.
	return	( ( objTempDate.getFullYear() == year  ) &&
			  ( objTempDate.getMonth()    == month ) &&
			  ( objTempDate.getDate()     == day)  )  ? true : false

	// Very nice method Dan!
}

function isTime (intHour, intMinute, intSecond)
{
	// Baased upon:   Dan Osborne <dano at specialist.co.uk>
	//                4/26/1999
	//                JavaScript <javascript at LaTech.edu>
	// Modified by:   Walter Torres <walter at torres.ws>
	//                4/29/2001

	// This is an interesting way to do validation of Time!

	// Create a new JS Date Object based upon given values.
	var objTempTime = new Date( 0, 0, 1, intHour, intMinute, intSecond );

	// Now, tear that new Data Object apart and see if the
	// values it gives back matches what we gave it.
	return	( ( objTempTime.getHours()   == intHour   )  &&
			  ( objTempTime.getMinutes() == intMinute ) &&
			  ( objTempTime.getSeconds() == intSecond ) ) ? true : false
}

====================================




More information about the Javascript mailing list