D wrote:
Hello all...I have an issue with one of my java script functions that
I'm hoping someone can easily help with. I have a web based application
that we use to create/sign up for overtime. When we have holidays we
require our employees to sign up in 4 hr increments for the times we
post. I'm having trouble creating a time slot that ends @ 12am. 12pm
and all other hours work fine for start/end times. however 12am causes
problems. My actual script code for this function is below. Someone
help!!!
I guess what you call a "slot" ending at 12am is something like 8pm to
midnight. I suspect the reall issue isn't creating the slot, but how
to display it so a user sees 8pm to midnight on a particular date,
rather than 8pm to 12am the following day.
I find the use of 12am and 12pm confusing, though I think most people
consider 12pm is noon and 12am is midnight. You could replace them with
noon/midday and midnight or use a 24hr clock, though without seeing
your screen layout I'm only guessing.
How you handle the 'real' time behind the scenes is up to you, but you
need to make it simple for people to select a date, then a time slot.
You could get the date by subtracting one second from the "to" time,
then use that date. For midnight, it will give you the date of the
preceeding day (i.e. the one before 00hrs). For all other "to" times,
it will give you the same day (assuming 4 hours slots).
<SCRIPT language="JavaScript" src="frmvalidation.js"
type="text/JavaScript"></SCRIPT>
The language attribute is deprecated, you can remove it. Keep type.
It doesn't make much sense to post a script element that references a
script file we can't access.
<script language="javascript">
function checkValidHoliday(value)
{
Please indent using 2 or 4 spaces, it reduces wrapping. Also, this
function is not called by your script, so not much point in including
it.
[...]
function checkHoliday(frm)
{
if(frm.txtIsHoliday.value=="Y")
That form control seems to be either a select or a text input, consider
using a checkbox:
<label for="txtIsHoliday"><input type="checkbox" id="txtIsHoliday"
name="txtIsHoliday">Is this for a holiday?</label>
Now you can use:
if (frm.txtIsHoliday.checked)
{
strFromDt= new Date(frm.txtLimitDate.value);
strToDt= new Date(frm.txtLimitToDate.value);
FromHr=parseInt(frm.txtLimitFrom.value);
FromAMPM=frm.txtLimitFromAM_PM.value;
ToHr=parseInt(frm.txtLimitTo.value);
ToAMPM=frm.txtLimitToAM_PM.value;
Why all the global variables? You should keep them local unless you
have a good reason for them to be global. You may have conflicts with
variables used in other (not posted) functions.
You also haven't posted anything to let us know what the values are.
If any are user-entered, you must validate them first. I'll presume
they are all selected from selects and you know they'll be OK.
if((FromAMPM=="PM") && (FromHr!=12))
FromHr=FromHr+12;
I guess these are all values selected from select elements, and that
you have one for am/pm and one for 0 to 12 hours. Why not use one
select with the text as four hourly intervals (or hourly if it's used
for other things too) from 0 am through noon to 12 midnight. Then set
the values using a 24 hour clock, e.g.
<select name="fromHr">
<option value="0">0 am (midnight)
<option value="4">4 am
<option value="8">8 am
<option value="12">12 noon
<option value="16">4 pm
<option value="20">8 pm
</select>
<select name="toHr">
<option value="4">4 am
<option value="8">8 am
<option value="12">12 noon
<option value="16">4 pm
<option value="20">8 pm
<option value="24">12 midnight
</select>
I find that less confusing, there is no need to select am/pm and
there's no conversion to 24hr clock. You need to validate the input to
ensure fromHr is before toHr (or deal with it some other way).
if((ToAMPM=="PM") && (ToHr!=12))
ToHr=ToHr+12;
var fromDt= new
Date(strFromDt.getFullYear(),strFromDt.getMonth(), strFromDt.getDate(),FromH*r,0);
var toDt= new
Date(strToDt.getFullYear(),strToDt.getMonth(),strT oDt.getDate(),ToHr,0);
//alert(toDt + " " + fromDt);
//alert(FromHr + " " +ToHr);
var
HolidayBlock=(parseInt(frm.cboHolidayHours.value) * 360 * 10000)
;
If you are converting from hours to milliseconds, the relevant values
are 3600 (seconds per hour) and 1000 (milliseconds per second) - your
values will end up with the right multiplier, but the individual values
are wrong. There is no need to use parseInt when multiplying a string
that (I guess) you know will have a suitable value, it will be
converted to a number by the multiplication. You might find it easier
to use 3.6e6. :-)
var HolidayBlock = frm.cboHolidayHours.value * 3.6e6;
Again, we have no idea what the value of HolidayBlock is - I'll guess
that it's "4".
//alert((toDt-fromDt) + " " + HolidayBlock );
if((toDt-fromDt)== HolidayBlock)//14400000)
return true;
else
{
alert("Please select "+
parseInt(frm.cboHolidayHours.value) +" Hour
It seems you are getting the value of a form control, which is a
string, using parseInt (unnecessarily) to convert it to a number, then
concatenating it to a string which will convert it back to a string.
Why bother with the conversion? There is also no need for the else
part - if the preceding if statement returns true, the function will
return and the following statement will not be executed.
Block only for Holiday!");
frm.txtLimitFrom.focus();
return false;
}
}
return true;
Why not start the script with:
if ( ! frm.txtIsHoliday.checked) return true;
and get rid of the outer if block.
Does your script deal effectively with changes to/from daylight saving?
Your explicit use of a four hour interval in milliseconds makes me
think it doesn't.
--
Rob