aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes How to calculate the time difference in javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to calculate the time difference in javascript?" Watch "How to calculate the time difference in javascript?" New topic
Author

How to calculate the time difference in javascript?

Akshay Madhuranath
Greenhorn

Joined: Nov 29, 2009
Posts: 20
Hello guys,
I’ve a question. I am trying to populate the javascript and want to return the value to the jsp page.. The function is like this.

function difference(startTime, endTime){
var starttime = document.getElementById(startTime).value;
var endtime = document.getElementById(endTime).value;

var stime = starttime.getHours();
var etime = endtime.getHours()
document.getElementById(startTime).value = stime
document.getElementById(endTime).value = etime

if(etime>=stime){
return (etime-stime);
}
else if(etime<=stime){
alert("enter valid time");
}


}


and this should return the control to the jsp page and should populate the difference. And the html inside jsp is


<tr>
<td >  Number of Hours                  
<input type="text" id="hour" name="hour" readonly size="4" onchange="difference('gsmastarttime', 'gsmaendtime')">

</td>
</tr>

This is a readonly text box. So i've the start time and endtime from another function and getting the values to difference function and also those values are present in the html embedded inside jsp. Its not printing the difference. So wat should i do inorder it prints the difference in the readonly text box?? Please help guys.
Thank in advance.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Don't return a value in your javascript function, but set the value of your text field



instead of



Bye,
Nicola
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Sorry, reading again your function i noticed another issue.

I don't think that onChange is the right event that could be triggered by your function if the textfield is readonly.
I mean, if the field is readonly, how could it trigger an onChange event?

What is the event you want to happen to change the value of your textfield?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
And once again...
why your javascript function gets two arguments that it never uses?

In few words, why you write



instead of



?

Last question:
Do you think that calling getHours() on a value of a textfield is going to work? You shoud at least trying to build a Date object with that value.
Akshay Madhuranath
Greenhorn

Joined: Nov 29, 2009
Posts: 20
Thanks for your reply. See i have 2 fields one is start time and another is end time. so i have to find out the difference between the end and the start time and print it in the text box. So for that i wrote the difference function.
Going forward, i changed the return as you mentioned and still its not able to find the difference. I have an onchange evet function for both starttime and endtime which gives me the utc times for that perticular time. So now i am trying to populate the difference and print it in the text box. So how to do it? please help. Thanks in advance..
Akshay Madhuranath
Greenhorn

Joined: Nov 29, 2009
Posts: 20
Nicola Garofalo wrote:And once again...
why your javascript function gets two arguments that it never uses?

In few words, why you write



instead of



?

Last question:
Do you think that calling getHours() on a value of a textfield is going to work? You shoud at least trying to build a Date object with that value.


These are used to get the values entered in the html page of the jsp. so i have done getelementbyid(of that time variable). that is

var starttime = document.getElementById(startTime).value;
var endtime = document.getElementById(endTime).value;


Yes that is correct. Thanks. I dont need to do .getHours() because it'll always return a number from 0 to 24.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
ok Akshay, so now may i see your javascript function again?
Please useCodeTags to write down your code, so that it's shown in a more readable way.

Pay attention to your sintax.

You can write



if endTime is the id of the textfield in your jsp

But if endTime textfield is the argument of your javascript function
you can just write

Akshay Madhuranath
Greenhorn

Joined: Nov 29, 2009
Posts: 20
Ok i got it. So i have made the changes you said.




Now i am returning this value to the jsp file of number of hours column. The html code is this.



Still its not showing the difference.

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Ok. Javascript function seems to be almost ok now. You can delete the empty alert call there, it's useless.

Now, ask yourself this question.
What event will trigger function difference?

As you wrote the javascript function you must have three text fields written more or less in the following way:



And




Do you agree? You must pass the entire textfield and not just a String to your javascript function difference

Now in the javascript function you must manage the cases of bad input text fields values.






Akshay Madhuranath
Greenhorn

Joined: Nov 29, 2009
Posts: 20
Nicola Garofalo wrote:Ok. Javascript function seems to be almost ok now. You can delete the empty alert call there, it's useless.

Now, ask yourself this question.
What event will trigger function difference?

As you wrote the javascript function you must have three text fields written more or less in the following way:



And




Do you agree? You must pass the entire textfield and not just a String to your javascript function difference

Now in the javascript function you must manage the cases of bad input text fields values.









I got it man. Thanks a lot for helping me..
cheers.
Akshay DM
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to calculate the time difference in javascript?