PDA

View Full Version : How do you display time with a leading 0? Please help



Mar
11-17-2004, 12:48 AM
I'm new to Javascript and I have this code working but I do not know how to display the hour, minute, or second value that has a single digit with a leading 0. Any idea's? The code is below.

Thank you in advance.

<html>
<head>
<title></title>


</head>
<body bgcolor="#A4D4B0">
<bgsound src ="clocktick2.wav" loop="2">


<form name="count">
<input type="text" size="69" name="count2">
</form>


<script>



var before="My Birthday!"
var current="Today is My Birthday!"
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown(yr,m,d){
theyear=yr;themonth=m;theday=d
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()


var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
futurestring=montharray[m-1]+" "+d+", "+yr
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
if(dday==0&&dhour==0&&dmin==0&&dsec==1){
document.forms.count.count2.value=current
return
}

else
document.forms.count.count2.value="Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left until "+before
setTimeout("countdown(theyear,themonth,theday)",1000)
}
//enter the count down date using the format year/month/day
countdown(2005,08,17)
</script>



</body>
</html> :confused:

Nate_Bro
11-17-2004, 02:35 AM
ya, i could not find i script for my web site, so i wrote this one :)



<head>
<title>Time and Date</title>
<style>
.texta {
font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
padding-left: 5px;
padding-top: 2px;
background: transparent;
color: #000000;
background-color: transparent;
border: transparent;
}
</style>
<script>

function DateDemo(){

d = new Date();
month = (d.getMonth() + 1);
day = d.getDate();
year = d.getYear();
s = "/"

if (day <= 9){
day = "0" + day
}
if (month <=9){
month = "0" + month
}
document.form.date.value = month + s + day + s + year
}

function timeload(){

{DateDemo()}

var day = " AM"
var d = new Date();
var t_hour = d.getHours();
var time = d.getHours();
var t_min = d.getMinutes();
var t_sec = d.getSeconds();
var t_mil = d.getMilliseconds();
var hour = t_hour-12

if (t_hour > 12 || t_hour == 1){
day = " PM"
}
if (t_hour <= 9){
t_hour = " "+t_hour
}
if (t_hour >= 13 && t_hour <=21){
t_hour = " "+hour
}
if (t_hour >= 13 && t_hour >=21){
t_hour = hour
}
if (t_min < 10){
t_min = "0"+t_min
}
if (t_sec < 10){
t_sec = "0"+t_sec
}
document.form.time.value = t_hour + ":" + t_min + ":"+ t_sec + day
setTimeout("timeload()", 10)
}
</script>
</head>
<body onload="timeload()" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
<form name="form">
<div style="border:2px solid #000000; position: absolute; width: 91px; height: 41px; z-index: 15; left: 45px; top: 47px" id="layer2">
<input class="texta" name="date" value="Date:" size="11" style="font-weight: 700"><input value="Time:" class="texta" name="time" size="11" style="font-weight: 700"></b></div>
</form>
</body>
</html>

Mar
11-17-2004, 02:54 AM
What?????

Nate_Bro
11-17-2004, 04:13 AM
sorry bout that, at first i thought you wanted just a clock, my bad

this is your script with the modifications

i just put this in it

//Added============\/-\/-\/-\/

if (dhour < 10){
dhour = "0"+dhour
}
if (dmin < 10){
dmin = "0"+dmin
}
if (dsec< 10){
dsec = "0"+dsec
}

//Added============/\-/\-/\-/\



<html>
<head>
<title></title>


</head>
<body bgcolor="#A4D4B0">
<bgsound src ="clocktick2.wav" loop="2">


<form name="count">
<input type="text" size="69" name="count2">
</form>


<script>



var before="My Birthday!"
var current="Today is My Birthday!"
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown(yr,m,d){
theyear=yr;themonth=m;theday=d
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec

futurestring=montharray[m-1]+" "+d+", "+yr
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000) )/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000 ))%(60*1000))/1000*1)

//Added============\/-\/-\/-\/

if (dhour < 10){
dhour = "0"+dhour
}
if (dmin < 10){
dmin = "0"+dmin
}
if (dsec< 10){
dsec = "0"+dsec
}

//Added============/\-/\-/\-/\


if(dday==0&&dhour==0&&dmin==0&&dsec==1){
document.forms.count.count2.value=current
return
}

else
document.forms.count.count2.value="Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left until "+before
setTimeout("countdown(theyear,themonth,theday)",1000)
}
//enter the count down date using the format year/month/day
countdown(2005,08,17)
</script>



</body>
</html>

Mar
11-17-2004, 05:14 AM
Nate_Bro,
Thank you so very much, you have been a great help. It works great!!!!!!!!
Mar

Nate_Bro
11-17-2004, 03:11 PM
any time :)