Forum Moderators: open

Message Too Old, No Replies

i just need help altering this script

how do i change it so you dont have to type "am" or "pm" ...

         

mylungsarempty

3:02 pm on Mar 8, 2004 (gmt 0)

10+ Year Member



The person i am delivering this script to has decided she only wants to type in the military hours, instead of the "am" or "pm" so, 1700 instead of 500pm ... how do i alter this code to do that?

<script type="text/javascript">
in_ = "830am";
out_ = "125pm";
in_ampm = in_.substring(in_.length-2);
in_minute = (in_.substring((in_.length-4),3) * 1);
in_hour = (in_.substring(0,(in_.length-4)) * 1) + (in_ampm=="pm"?12:0);

out_ampm = out_.substring(out_.length-2);
out_minute = (out_.substring((out_.length-4),3) * 1);
out_hour = (out_.substring(0,(out_.length-4)) * 1) + (out_ampm=="pm"?12:0);

foobar = new Date();

in_timeObject = new Date(foobar.setHours(in_hour));
in_timeObject = in_timeObject.setMinutes(in_minute);
// in_timeObject is now a Unix timestamp
// let's do the same with the out time
out_timeObject = new Date(foobar.setHours(out_hour));
out_timeObject = out_timeObject.setMinutes(out_minute);
// now you just need to subtract...
timeDiff = (out_timeObject - in_timeObject)/1000;
// timeDiff holds the difference (in seconds) between the "in" and "out" times
document.write(in_hour + " h<br>" + in_minute + " m<br>" + in_ampm + "<br>" + out_hour + " h<br>" + out_minute + " m<br>" + out_ampm + "<br>" + in_timeObject + "<br>" + out_timeObject + "<br>" + timeDiff + "<br>")
</script>

Purple Martin

10:34 pm on Mar 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Get rid of both these bits:

+ (in_ampm=="pm"?12:0);

mylungsarempty

1:55 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



what is wrong with this script that makes 10 and 11 notwork as hours? it affects the minutes.

for example:

if you enter in: 900am and out:1030am, you will get a result of 1.05, instead of 1.5

the part of the script that calculates the minutes is getting screwed up from the extra digit, but i am not sure how to fix it! any help out there in internet land?

DrDoc

4:34 pm on Mar 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry. My bad!
The substring() calls were messed up. Here is a fixed version that works both with and without am/pm attached to the time (as long as both times are following the same convention):
<script type="text/javascript">
in_ = "830pm";
out_ = "1130pm";
if(in_.length>4) {
in_ampm = in_.substring(in_.length-2);
in_minute = (in_.substring((in_.length-4),(in_.length-2)) * 1);
in_hour = (in_.substring(0,(in_.length-4)) * 1) + (in_ampm=="pm"?12:0);

out_ampm = out_.substring(out_.length-2);
out_minute = (out_.substring((out_.length-4),(out_.length-2)) * 1);
out_hour = (out_.substring(0,(out_.length-4)) * 1) + (out_ampm=="pm"?12:0);
}
else {
in_minute = (in_.substring((in_.length-2)) * 1);
in_hour = (in_.substring(0,(in_.length-2)) * 1);

out_minute = (out_.substring((out_.length-2)) * 1);
out_hour = (out_.substring(0,(out_.length-2)) * 1);
}

foobar = new Date();

in_timeObject = new Date(foobar.setHours(in_hour));
in_timeObject = in_timeObject.setMinutes(in_minute);
// in_timeObject is now a Unix timestamp
// let's do the same with the out time
out_timeObject = new Date(foobar.setHours(out_hour));
out_timeObject = out_timeObject.setMinutes(out_minute);
// now you just need to subtract...
timeDiff = (out_timeObject - in_timeObject)/(60*60*1000);
// timeDiff holds the difference (in hours) between the "in" and "out" times
document.write(in_.length + " chars<br>" + in_hour + " h<br>" + in_minute + " m<hr>" + out_.length + " chars<br>" + out_hour + " h<br>" + out_minute + " m<hr>" + in_timeObject + "<br>" + out_timeObject + "<br>" + timeDiff + "<br>");
</script>

mylungsarempty

6:59 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



oh that is EXCELLENT. You're a lifesaver, DrDoc!

Just one quick question, if ya don't mind -- i've altered the script to suit my needs, and all i'm wondering is how i turnicate a number after two decimal places in javascript... or actually, i'd like to round it up or down to the nearest 100th, so all results end up like 40.04 instead of say 40.03333333333333333 heh, you know?

ajkimoto

7:16 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



mylungsarempty,

One way to do this is:

//number of zeroes in numDec is number of decimal places that you want

var mynum=8.333333333333
var numDec=100
var mynum2=Math.round(mynum*numDec)/numDec

ajkimoto

DrDoc

7:44 pm on Mar 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or, use sprintf():
function sprintf() { 
var iCount, iPadLength, aMatch, iMatchIndex = 1;
var bAlignLeft, sPad, iWidth, iPrecision, sType;
var aArgs = sprintf.arguments;
if(aArgs.length < 2) {
return '';
}

var sFormat = aArgs[0];
var re = /%(-)?(0¦ ¦'.)?(\d+)?(\.\d*)?([bcdfosxX]{1})/;
while(re.test(sFormat)) {
aMatch = re.exec(sFormat);
bAlignLeft = (aMatch[1] == '-');
sPad = (aMatch[2] == ''? ' ' : aMatch[2]);
if(sPad.substring(0, 1) == \"'\") {
sPad = sPad.substring(1);
}
iWidth = (aMatch[3] > 0? parseInt(aMatch[3]) : 0);
iPrecision = (aMatch[4].length > 1? parseInt(aMatch[4].substring(1)) : 6);
sType = aMatch[5];
mArgument = (aArgs[iMatchIndex]!= null? aArgs[iMatchIndex] : '');
++iMatchIndex;
if(mArgument.toString().length) {
if('fbcdoxX'.indexOf(sType)!= -1 && isNaN(mArgument)) {
mArgument = 0;
}
switch(sType) {
case 'f': // floats
var iPower = Math.pow(10, iPrecision);
mArgument = (Math.round(parseFloat(mArgument) * iPower) / iPower).toString();
var aFloatParts = mArgument.split('.');
if(iPrecision > 0) {
if(aFloatParts.length == 1) {
aFloatParts[1] = '';
}
// pad with zeroes to precision
for(iCount = aFloatParts[1].length; iCount < iPrecision; iCount++) {
aFloatParts[1] += '0';
}
mArgument = aFloatParts[0] + '.' + aFloatParts[1];
}
else {
mArgument = aFloatParts[0];
}
iPadLength = aFloatParts[0].length;
break;
case 'b': // binary
mArgument = parseInt(mArgument).toString(2);
iPadLength = mArgument.length;
break;
case 'c': // character
mArgument = String.fromCharCode(parseInt(mArgument));
break;
case 'd': // decimal
mArgument = mArgument.toString();
iPadLength = mArgument.length;
break;
case 'o': // octal
mArgument = parseInt(mArgument).toString(8);
iPadLength = mArgument.length;
break;
case 'x': // hexadecimal (lowercase)
mArgument = parseInt(mArgument).toString(16);
iPadLength = mArgument.length;
break;
case 'X': // hexadecimal (uppercase)
mArgument = parseInt(mArgument).toString(16).toUpperCase();
iPadLength = mArgument.length;
break;
default: // strings
mArgument = mArgument.toString();
iPadLength = mArgument.length;
}
if('fbdoxX'.indexOf(sType)!= -1) {
// pad with padding-char to width
if(bAlignLeft) {
for(iCount = iPadLength; iCount < iWidth; iCount++) {
mArgument += sPad;
}
}
else {
for(iCount = iPadLength; iCount < iWidth; iCount++) {
mArgument = sPad + mArgument;
}
}
}
}
sFormat = sFormat.replace(re, mArgument);
}
return sFormat;
}

mylungsarempty

8:05 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



i like ajkimoto's way better ;D

Everybody's help is so appreciated :)

DrDoc

9:07 pm on Mar 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the purpose of what you want to do -- yes, I like ajkimoto's way better too. ;) But, that little sprintf function is a keeper, since that's the only way you can get sprintf functionality in JavaScript (right now).