Displaying Data with Forms in JavaScript


Display today's date in a text input box.

function getDate ()
{
	var today = new Date();
	document.date.formDate.value	= (today.getMonth() + 1)
					+ "/"
					+ today.getDate()
					+ "/"
					+ today.getYear();
}

Display the current time in a text input box.

var clock = 0;

function startClock()
{
	clock = setTimeout("tick()", 500);
}

function endClock()
{
	if (clock)
	{
		clearTimeout (clock);
		clock  = 0;
	}
}

function tick()
{
	var today	= new Date();
	var hours	= today.getHours();
	var minutes	= today.getMinutes();
	var seconds	= today.getSeconds()

	if (clock)
	{
		clearTimeout (clock);
		clock = 0;
	}

        var time = "" + ((hours >12) ? hours -12 :hours)
        time	+= 	((minutes < 10) ? ":0" : ":") + minutes
        time	+= 	((seconds < 10) ? ":0" : ":") + seconds
        time	+= 	(hours >= 12) ? " pm" : " am"

	document.time.formTime.value = time;
	clock = setTimeout("tick()", 1000);
}


Count-down to the year 3000.

Note that this count-down is inaccurate since it assumes that every month has 31 days.

years, months, days, hours, minutes, seconds to the year 3000!!!
function startCountDown ()
{
	endCountDown ();
	showCountDown ();
}

function showCountDown ()
{
	var today	= new Date();

	var month	= today.getMonth();
	var day		= today.getDay();
	var year	= today.getYear();

	var hour	= today.getHours();
	var minute	= today.getMinutes();
	var second	= today.getSeconds();

	today 		= null;

	hours		= 23 - hour;
	minutes		= 59 - minute;
	seconds		= 59 - second;
	var years	= 3000 - year;
	var months	= 11 - month;
	var days	= 31 - day;

	document.countDown.years.value		= years;
	document.countDown.months.value 	= months;
	document.countDown.days.value		= days;
	document.countDown.hours.value		= hours;
	document.countDown.minutes.value	= minutes;
	document.countDown.seconds.value	= seconds;

	countDown = setTimeout("showCountDown ()",1000);
	countDownStarted = true;
}

function endCountDown ()
{
	if (countDownStarted)
	{
		clearTimeout (countDown);
		countDownStarted = false;
	}
}