View Full Version : Validate Time
BubbleStar
03-29-2003, 06:55 AM
Hi all..
I need to know how to get the current time(be it according to server or client) and be displayed on client's browser.
i also have a combo box to allow user to select the time but they can only select the time AFTER the present time.. so, how do i validate it??
for example if the time now is 10am, user can only select from 10am onward from the combo box..
Please give me some suggestions...
thanx!:o
tpeck
03-29-2003, 08:05 AM
Displaying date/time in Javascript is fairly easy:
<script language="javascript">
<!--
var date;
var day_array = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var month_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
function show_time()
{
date = new Date();
day = day_array[date.getDay()];
maand = month_array[date.getMonth()];
minutes = date.getMinutes();
seconds = date.getSeconds();
if (minutes < 10 ) {minutes = "0" + minutes;}
if (seconds < 10) {seconds = "0" + seconds;}
obj_table_time = document.getElementById("table_time");
obj_table_time.innerHTML = " " + day + " " +date.getDate()+ " " + maand +" < " +date.getHours() + ":" + minutes +":" + seconds +" >";
setTimeout("show_time()",1000);
}
// -->
</script>
Just call the function show_time()
The second part of your question, I cannot help you sorry to give you false hope.
Terry
HairyTeeth
03-30-2003, 07:56 AM
See if any of this helps. It's not a rolls-royce job (it could be condensed a fair bit) but it should help a bit.
<html>
<head>
<title>Date - Time Example</title>
<script language="JavaScript" type="text/javascript">
<!-- ;
var dtmTimer,cboTimer,theTime,theDate,theMerideum;
//MAKE SURE THE ARRAY VALUES ARE ON ONE LINE
var dayArray = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var mthArray = new Array('January','Febuary','March','April','May','June','July','August','September','October','Novemb er','December');
// Syncronize minutes betweem combox and live clock;
function syncMin(){
var now = new Date();
var s = now.getSeconds();
var x = (60 - s);
var d = 0;
if(x>0)d = x*1000;
if(x==0)d = 1000*60;
return d;
}
// combobox function;
function cboFunc(){
var delay = syncMin();
var now = new Date();
var theMin = now.getMinutes();
var nextHour = now.getHours() + 1;
if(theMin < 10) theMin = "0"+theMin;
if(nextHour > 12){
nextHour = nextHour -12;
theMerideum="pm";
} else{
theMerideum="am";
}
var page="";
page+="<form name='frmTime'>";
page+="<select name='optList'>";
for(var i=0;i<5;i++){
page+="<option name='opt_'"+[i]+"' value="+(nextHour + i) + ":" + theMin + theMerideum + ">"
page+=(nextHour + i) + ":" + theMin + theMerideum+ "</option>";
}
page+="</select>";
page+="<input type='button' value='Select' onclick='myFunc(this.form)'>";
page+="</form>";
document.getElementById('cboLyr').innerHTML = page;
cboTimer = setTimeout("cboFunc()",delay);
}
////////////////////////////////////////////////////////////////////////////////////////;
//date and live clock function;
function dtmFunc() {
var now = new Date();
var theDay = now.getDate();
var theWeekDay=dayArray[now.getDay()];
var theMonth=mthArray[now.getMonth()];
var theYear = now.getFullYear();
var theHour = now.getHours();
var theMin = now.getMinutes();
var theSec = now.getSeconds();
if(theSec<10) theSec = "0" + theSec;
if(theMin<10) theMin = "0" + theMin;
if(theHour > 12){
theMerideum = "pm";
theHour = theHour - 12;
}else{
theMerideum = "am";
}
theTime = theHour + ":"+theMin + ":" + theSec + theMerideum;
theDate = theWeekDay +", "+theMonth+" "+ theDay + ", " + theYear;
document.getElementById('dtmBox').innerHTML = theDate + "<br />" + theTime;
}
dtmTimer = setInterval("dtmFunc()",1000);
////////////////////////////////////////////////////////////////////////////
//Example function when the form button is clicked.
function myFunc(form){
var result = "";
for(var j = 0; j<form.optList.length; j++){
if(form.optList.options[j].selected){
result="\n" + form.optList.options[j].value;
}
}
alert("The time you have selected is: " + result);
}
// end hide -->
</script>
</head>
<body onload="cboFunc()">
<h1>Combo Box Demo</h1>
<div id="dtmBox" style="position:relative;left:0px;top:0px;"></div>
<div id="cboLyr" style="position:relative;left:0px;top:0px;"></div>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.