Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-19-2008, 06:43 AM   PM User | #1
SonicBoomAu
New to the CF scene

 
Join Date: Jun 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SonicBoomAu is an unknown quantity at this point
Difference in Time

Hi All,

I am extremely new to Java programming and HTML, so be gentle =P

I have a form where the user can select a start and finish time.

HTML code
Code:
<input type="text" id="startTime" name="startTime" /><a href="javascript:getTime('startTime')><img></img></a>

<input type="text" id="finishTime" name="finishTime" /><a href="javascript:getTime('finishTime')><img></img></a>
Java code
Code:
function getTime(pElem){
    myDate = new Date();
    
    hours = myDate.getHours();
    minutes = myDate.getMinutes();

    document.getElementById(pElem).value = hours+":"+minutes;
What I am after is help with finding out the difference between these to values.

My HTML code so far
Code:
<input type="text" id="totalTime" name="totalTime" /><a href="javascript:timeDiff('totalTime')><img></img></a>
I have tried a couple of different things for the Java but to no avail.

All help is extremely welcome.

SBA
SonicBoomAu is offline   Reply With Quote
Old 06-19-2008, 08:31 AM   PM User | #2
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
Let me answer this by stating that Java is different from Javascript.

First mistake is this typo, which goes on all your inline event handler (onclick):
Code:
<a href="javascript:getTime('startTime')">
See if this code helps:
Code:
<script type="text/javascript">
var startTimeH,startTimeM,finishTimeH,finishTimeM;   
function getTime(pElem)
{
var myDate = new Date(),
hours = myDate.getHours(),
minutes = myDate.getMinutes();
document.getElementById(pElem).value = hours+":"+minutes;
if(pElem=='startTime')
	{
	startTimeH=hours;
	startTimeM=minutes;
	}
else if(pElem=='finishTime')
	{
	finishTimeH=hours;
	finishTimeM=minutes;
	}
}
function timeDiff(test)
{
var iSSeconds = (startTimeH*3600)+(startTimeM*60),
iESeconds = (finishTimeH*3600)+(finishTimeM*60),
iDif = (iESeconds-iSSeconds)/60,
filtH=parseInt(iDif/60),
filtS=parseInt(iDif%60);
document.getElementById(test).value = filtH +':'+filtS;
}
</script>
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Users who have thanked rangana for this post:
SonicBoomAu (06-19-2008)
Old 06-19-2008, 08:34 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
As rangana says, Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

I am not clear whether your form already validates the times entered, but this is a complete solution:-

Code:
<form name = "myform">
Enter the start time in HH:MM format <input type="text" id="startTime" name="startTime" size = "6" onblur = "chkstart()" ><br>
Enter the finish time in HH:MM format <input type="text" id="finishTime" name="finishTime" size = "6" onblur = "calcDiff()"><br>
Total time:- <input type = "text" id = "timeDiff" name = "timeDiff" size = "6">
</form>

<script type = "text/javascript">

var st;
var stsplt;
var ftsplt;
var passflag;

function chkstart () {
var flag = 0;
st = document.myform.startTime.value;
st = st.replace(/[\.-]/,":");  // allow . or - separator
if (!/\d{1,2}:\d{2}/.test(st)) {  // allow 1 or 2 digits in hours, only 2 in minutes
flag ++;
}
stsplt = st.split(":");  // split hours and minutes
stsplt[0] = stsplt[0] * 1;  // Make number.  parseInt(stsplt[0]) would do same thing
stsplt[1] = stsplt[1] * 1;
if ((stsplt[0] < 0) || (stsplt[0] > 23)) {  // valid hour 0-23
flag ++;
}
if ((stsplt[1] < 0) || (stsplt[1] > 59)) {  // valid minutes 0-59
flag ++;
}
if (flag > 0) {
alert ("Invalid time - please re-enter! ");
flag = 0;  // reset flag
document.myform.startTime.value = "";  // clear the field
document.myform.startTime.focus();  // and focus on it
return false;
}
else {
passflag = 1;  // valid start time has been entered
}
}

function calcDiff () {
if (passflag != 1) {
alert ("You must enter a starting time!" );
document.myform.startTime.value = "";
document.myform.startTime.focus();
return false;
}
var flag = 0;
var ft = document.myform.finishTime.value;
ft = ft.replace(/[\.-]/,":");
if (!/\d{1,2}:\d{2}/.test(ft)) {
flag ++;
}
ftsplt = ft.split(":");
ftsplt[0] = ftsplt[0] * 1;
ftsplt[1] = ftsplt[1] * 1;
if ((ftsplt[0] < 0) || (ftsplt[0] > 23)) {
flag ++;
}
if ((ftsplt[1] < 0) || (ftsplt[1] > 59)) {
flag ++;
}
if (ftsplt[0] < stsplt[0]) {
flag ++;
}
if ((ftsplt[0] == stsplt[0]) && (ftsplt[1] > stsplt[1])) {
flag ++;
}

if (flag > 0) {
alert ("Invalid time - please re-enter! ");
flag = 0;
document.myform.finishTime.value = "";
document.myform.finishTime.focus();
return false;
}

var hrsDiff = ftsplt[0] - stsplt[0];
if (ftsplt[1] < stsplt[1]) {
hrsDiff = hrsDiff -1;
}

var minsDiff = ftsplt[1] - stsplt[1];
if (minsDiff < 0) {
minsDiff = 60 + minsDiff;
}
if (minsDiff < 10) {
minsDiff = "0" + minsDiff;
}

var diffTime = hrsDiff + ":" + minsDiff;
document.myform.timeDiff.value = diffTime;

}

</script>


Quizmaster: Three stumps with two bails on top are essential equipment in which sport?
Contestant: Horse racing.

Last edited by Philip M; 06-19-2008 at 10:35 AM.. Reason: Improved
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
SonicBoomAu (06-20-2008)
Old 06-19-2008, 11:25 PM   PM User | #4
SonicBoomAu
New to the CF scene

 
Join Date: Jun 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SonicBoomAu is an unknown quantity at this point
Thats for the help guys and correcting my programming language mistake.

I will try out rangana suggestion and let you know how I go.

Cheers
SonicBoomAu is offline   Reply With Quote
Old 06-20-2008, 12:07 AM   PM User | #5
SonicBoomAu
New to the CF scene

 
Join Date: Jun 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SonicBoomAu is an unknown quantity at this point
Have implemented the above suggestion from Rangana, but I end up with a result of NaN:NaN.

What am I doing wrong?

TIA

*EDIT*

For some reason or other the timeDiff function wasn't about to see the startTimeH / FinTimeH / startTimeM / FinTimeM values, to over come this I used the code in the next example.

Last edited by SonicBoomAu; 06-20-2008 at 01:10 AM.. Reason: Found problem
SonicBoomAu is offline   Reply With Quote
Old 06-20-2008, 01:07 AM   PM User | #6
SonicBoomAu
New to the CF scene

 
Join Date: Jun 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SonicBoomAu is an unknown quantity at this point
Hi again Guys,

Thank you both for your time and examples. In the end I used bits and pieces from both of you. The JAVASCRIPT (=P) ended up as follows.
Code:
function timeDiff(test)
{
    var st = document.review.startTime.value;
    var ft = document.review.finTime.value;

    // split the values
    var stsplt;
    var ftsplt;
    var startTimeH;
    var finTimeH;
    var startTimeM;
    var finTimeM;

    // capture Start Time HH & MM
    stsplt = st.split(":");
    startTimeH = stsplt[0];
    startTimeM = stsplt[1];

    // capture Finish Time HH & MM
    ftsplt = ft.split(":");
    finTimeH = ftsplt[0];
    finTimeM = ftsplt[1];

    var iSSeconds = (startTimeH*3600)+(startTimeM*60);
    iESeconds = (finishTimeH*3600)+(finishTimeM*60);
    iDif = (iESeconds-iSSeconds)/60;
    filtH=parseInt(iDif/60);
    filtS=parseInt(iDif%60);

    // Just to display the time in a nice format
    if (filtH < 10) filtH= "0" + filtH;
    if (filtS< 10) filtS= "0" + filtS;

    // Place difference in value box.
    document.getElementById(test).value = filtH +':'+filtS;
}
Once again Thank you both for your help.

I hope that in the future this can help someone else.
SonicBoomAu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:21 AM.


Advertisement
Log in to turn off these ads.