Date object problems

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
internalgrenade
Forum Newbie
Posts: 5
Joined: Mon Oct 04, 2010 7:29 pm

Date object problems

Post by internalgrenade »

I'm Trying To Create Custom Date Object.
but am comeing back with a NaN error.

Source:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Alarm</title>
</head>
<style type="text/css">
</style>
<script type="text/javascript">
function startup() {
curdate = new Date();
h = curdate.getHours();
m = curdate.getMinutes();
s = curdate.getSeconds();
time = h + ":" + m + ":" + s;
AS_h = document.getElementById('input_hr').value;
AS_m = document.getElementById('input_mn').value;
AS_s = document.getElementById('input_sc').value;
AV_h = parseInt(AS_h);
AV_m = parseInt(AS_m);
AV_s = parseInt(AS_s);
alarmtime = AV_h + ":" + AV_m + ":" + AV_s;
document.getElementById('curtime').innerHTML=time;
setTimeout("startup()",1000);
document.getElementById('alarmtime_').innerHTML=alarmtime;
}
</script>
<body onLoad="startup()">
<span style="font-size:40px;">
Current Time:
</span>
<br />
<span style="font-size:40px;" id="curtime"></span>
<br />
<span id="alarmtime_"></span>
<hr />
<input type="text" class="" id="input_hr">
<input type="text" class="" id="input_mn">
<input type="text" class="" id="input_sc">
<br />
<input type="button" value="+1hr" onClick="">
<input type="button" value="+10min" onClick="">
<input type="button" value="+60sec" onClick="">
<input type="button" value="Start Alarm" onClick"alarmset()">
</body>
</html>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Date object problems

Post by McInfo »

BBCode syntax tags, please! :evil:

NaN means "Not a Number". The time inputs have no value attributes, so on page load, their default values are empty strings. When the parseInt() function is given an empty string as input, it returns NaN.

I'm not sure what the rationale is for taking a string input and converting it into a number only to convert that number back into a string. Are you sure you need parseInt()?

If you want the default alarmtime to be...
  • "NaN:NaN:NaN", do nothing.
  • "::", remove the calls to parseInt().
  • "0:0:0", add a value="0" attribute to each time input.
  • something else, ask.
Post Reply