Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Problem in Flag Operations

1. ## Problem in Flag Operations

I have wrote a code for some calculation that checks whether the RUN_time of an application falls between the Start_time and Stop_time, If it falls between the Start_time and Stop_time, then an alert with Success Message should popup else failure alert should popup.

I dont know what's the problem with the code, its not displaying the correct alert.

Can some experts deal with this and correct the code.

Code:
```<html>

<script type="text/javascript">
function convert12to24(timeStr)
{
var meridian = timeStr.substr(timeStr.length-2).toLowerCase();
var hours    = timeStr.substring(0, timeStr.indexOf(':'));
var minutes  = timeStr.substring(timeStr.indexOf(':')+1);
return hours+':'+minutes;
}

//This function only for testing
function changeTimeFormat()
{
var input = document.getElementById('input').value
var twenny4hr = convert12to24(input);
//document.getElementById('output').value = twenny4hr;
var input_hr = twenny4hr.substring(0, twenny4hr.indexOf(':'));
var input_min = twenny4hr.substring(twenny4hr.indexOf(':')+1);

if(input_hr >= 24)
{
alert('Hours should fall between 0:00  to 23:59');
softclose.input.value='';
}

if(input_min > 59)
{
alert('Minutes should fall between 00 to 59');
softclose.input.value='';
}

if(input_hr == 12)
{
var twelvehr = parseInt(input_hr);
document.getElementById('output').value = twelvehr + ':' + parseInt(input_min) + ' ' + 'P.M.';
}
else if(input_hr > 12 )
{
var twelvehr = parseInt(input_hr)-12;
document.getElementById('output').value = twelvehr + ':' + parseInt(input_min) + ' ' + 'P.M.';
}
else if(input_hr == 12 && input_hr == 0 || input_hr == 00  )
{
var twelvehr = parseInt(input_hr) + 12;
document.getElementById('output').value = twelvehr + ':' + parseInt(input_min) + ' ' + 'A.M.';
}
else
{
var twelvehr = parseInt(input_hr);
document.getElementById('output').value = twelvehr + ':' + parseInt(input_min) + ' ' + 'A.M.';
}

}

function changeTimeFormat1()
{
var t_bus_clndr_gmt = document.getElementById('t_bus_clndr_gmt').value
var twenny4hr = convert12to24(t_bus_clndr_gmt);
//document.getElementById('output1').value = twenny4hr;
var t_bus_clndr_gmt_hr = twenny4hr.substring(0, twenny4hr.indexOf(':'));
var t_bus_clndr_gmt_min = twenny4hr.substring(twenny4hr.indexOf(':')+1);

if(t_bus_clndr_gmt_hr >= 24)
{
alert('Hours should fall between 0:00  to 23:59');
softclose.t_bus_clndr_gmt.value='';
}

if(t_bus_clndr_gmt_min > 59)
{
alert('Minutes should fall between 00 to 59');
softclose.t_bus_clndr_gmt.value='';
}

if(t_bus_clndr_gmt_hr == 12)
{
var twelvehr = parseInt(t_bus_clndr_gmt_hr);
document.getElementById('output1').value = twelvehr + ':' + parseInt(t_bus_clndr_gmt_min) + ' ' + 'P.M.';
}
else if(t_bus_clndr_gmt_hr > 12 )
{
var twelvehr = parseInt(t_bus_clndr_gmt_hr)-12;
document.getElementById('output1').value = twelvehr + ':' + parseInt(t_bus_clndr_gmt_min) + ' ' + 'P.M.';
}
else if(t_bus_clndr_gmt_hr == 12 && t_bus_clndr_gmt_hr == 0 || t_bus_clndr_gmt_hr == 00  )
{
var twelvehr = parseInt(t_bus_clndr_gmt_hr) + 12;
document.getElementById('output1').value = twelvehr + ':' + parseInt(t_bus_clndr_gmt_min) + ' ' + 'A.M.';
}
else
{
var twelvehr = parseInt(t_bus_clndr_gmt_hr);
document.getElementById('output1').value = twelvehr + ':' + parseInt(t_bus_clndr_gmt_min) + ' ' + 'A.M.';
}

//EST Format
var min = twenny4hr.substring(twenny4hr.indexOf(':')+1);
var esthour = parseInt(twenny4hr)-4;

if(esthour > 12)
{
document.getElementById('EST').value = esthour + ':' + parseInt(min);
}
else if(esthour < 0)
{
document.getElementById('EST').value = esthour + 12 + ':' + parseInt(min);
}
else
{
document.getElementById('EST').value = esthour + ':' + parseInt(min);
}
}

function open_close_window()
{
var est = document.getElementById('EST').value;
var min = est.substring(est.indexOf(':')+1);

var ow = document.getElementById('open_window').value;
var cw = document.getElementById('close_window').value;

var est_hour    = est.substring(0, est.indexOf(':'));
var start_time = parseInt(est_hour - ow);
var stop_time = parseInt(est_hour) + parseInt(cw);
document.getElementById('start_t').value =  start_time + ':' + parseInt(min);
document.getElementById('end_t').value = stop_time + ':' + parseInt(min);
}

function check1()
{
var user_req_time = document.getElementById('input').value;
var user_req_hours = user_req_time.substring(0, user_req_time.indexOf(':'));

var start_time1 = document.getElementById('start_t').value;
var start_hours    = start_time1.substring(0, start_time1.indexOf(':'));

var stop_time1 = document.getElementById('end_t').value;
var stop_hours    = stop_time1.substring(0, stop_time1.indexOf(':'));

if(start_hours < user_req_hours )
{
flag = 0;
}
else
{
flag=1;
}

if(stop_hours > user_req_hours)
{
flag1 = 0;
}
else
{
flag1=1;
}

if(flag == 0 && flag1 ==0)
{
}
else
{
}

}

</script>

<body>
<form name="softclose">
<table>
<tr>
<td>
User Requirement [24 hr format] : </td>
<td> <input type="text" name="input" id="input"  onkeyup="changeTimeFormat();"> EST  </td>
<td>
12Hr Output : </td><td> <input type="text" name="output"  id="output"></td>
</tr>

<tr>
<td>
TBUS_CLNDR DB Time [24 hr format] : </td>
<td><input type="text" name="t_bus_clndr_gmt"  id="t_bus_clndr_gmt" onkeyup="changeTimeFormat1();"> GMT </td>
<td>
12Hr Output : </td>
<td><input type="text" name="output1"  id="output1">
</td>
</tr>

<tr>
<td width=200>  Open Window  </td>
<td width=120 colspan=2><input type='text' name="open_window" id='open_window' size=5 onkeyup="open_close_window();"//> Hrs </td>
</tr>

<tr>
<td width=200>  Close Window  </td>
<td width=120 colspan=2><input type='text' name="close_window" id='close_window'  size=5 onkeyup="open_close_window();"/> Hrs </td>
</tr>
</table>
<br><br><br>

<table>
<tr>
<td width=200> EST Format: </td>
<td ><input type="text" name="EST"  id="EST" value="" > EST</td>
</tr>

<tr>
<td width=200> Start Time: [24 hr format]</td>
<td ><input type="text" name="start_t"  id="start_t" value="" ></td>
</tr>

<tr>
<td width=200> End Time: [24 hr format]</td>
<td><input type="text" name="end_t"  id="end_t" value="" ></td>
</tr>

<tr>
<td align=center>
<input type=button name=check id=check value=check onclick="check1();">
</td>
</tr>

<table>

</form>
</body>
</html>```

• I don't really understand what this (very convoluted) code is supposed to do, but when I tested it I got the "success" message - but the end time showed 25:34

Variable names such as var t_bus_clndr_gmt_hr are hard to read and prone to typo errors.

My advice would be to put alerts into the script at strategic intervals so that you can observe the values of the variables as the calculations progress.

I don't see anthing to test whether the values entered by the users are numbers. Or to convert a delimiter other than : to :. Values entered by the user into a textbox are strings unless converted to numbers. So "100" is less than "20". So test thoroughly with "silly" values.

Quizmaster: In astronomy, a nucleus, a coma and a tail are parts of which celestial body?
Contestant: A horse.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•