954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

time calculations

How can I get the difference of two time values using javascript?

shemayb
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Take a look at the functions provided by the Date object of Javascript and try your own hand at it. Post your attempt with the problems you are facing if you are stuck.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

i tried using the parse method but it didn't work.My case is this: i have a time in and a time out, i want to subtract my time out to my time in so that i can get the total difference of the two.Only time,it does not include the date.

shemayb
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

It depends a lot on the format in which the times are expressed. Post some code so that we have something to chew on.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

here is my sample code:

var end =Date.parse(etime);
var start=Date.parse(stime);

var total =(end-start);
if (total > 2)

{
//alert message here
}
else
{
//link here
}

shemayb
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Several questions:

1. What format are the variables "etime" and "stime" in?

2. What do you want the value "2" to represent? It's not seconds or days. It is supposed to be milliseconds, but some implementations use jiffies instead (multiples of the Intel 55 ms interrupt period).

3. What units did you expect "total" to be in? You don't have any guarantee of what unit of measure the variable "total" is in. It is an internal time unit that depends on the computer used.

4. Do you really want to measure 2 ms times? Windows computers can't measure such times without special hardware.

Note that the system clock does not update each millisecond. Usually the system clock is updated once every 55ms jiffy. If you are really trying to measure intervals of 2 ms, you are doomed to failure. I/O itself happens only once every 55 ms on Windows computers.

DOS computers could do this. Windows computers can't.

One thing you could do is make a standard for comparison:

std = knownEndTime - knownStartTime;   // set a standard time interval for your comparison.
diffo = end - start;

if (diffo > std) {

// code for diffo is greater than the standard interval

}
else {

// code for diffo is less than or equal to the standard interval

}
MidiMagic
Nearly a Senior Poster
3,319 posts since Jan 2007
Reputation Points: 730
Solved Threads: 182
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You