Hii,
I have an input time in millisecounds. I want to include a digital stop watch in my application.i.e The time will dynamically change like a digital clock in every seconds.
Hii,
I have an input time in millisecounds. I want to include a digital stop watch in my application.i.e The time will dynamically change like a digital clock in every seconds.
You can divide the milliseconds by 1000 to get the seconds, then by 60 to get the minutes and by 60 again to get the hours. Or, even better, use modulo:
hours = parseInt(milliseconds / 3600) % 24;
minutes = parseInt(milliseconds / 60) % 60;
seconds = (milliseconds / 1000) % 60;
However if you want a time like 02:00 PM you must know from what time you started to count the milliseconds (ie, what time is it when the milliseconds are "0").
The prefix milli is 10^-3. Thus one millisecond is one thousandth of a second, 60 thousandth of a minute and 3600 thousandth of an hour. So 1,000 milliseconds is one second, 60,000 milliseconds is one minute and 3,600,000 milliseconds is one hour.
This means, devide the number of milliseconds by 1000 and you get the number of seconds, by 60,000 and you get the number of minutes, and by 3,600,000 and you get the number of hours.
I'm not sure exactly what your question is, as in what specific parts you need help with. Even if you're not that familiar with Javascript, this is fairly simple to do "manually" by just dividing by each increasing factor and taking the remainder, e.g.:
var input = ...; // your input time
var millis = input % 1000;
input /= 1000;
var seconds = input % 60;
input /= 60;
var minutes = input % 60;
input /= 60;
var hours = input % 24; // I presume this will be less than 24 anyway)
var entireTime = hours + ':' + minutes + ':' + seconds;
An alternate way to do this would be to create a Date object passing the input time into the constructor; this would then represent that number of milliseconds past the epoch and so printing out its value would include the given time. Depending on what date formatting frameworks you have available this might be a more straightforward method - and it would certainly allow more flexibility in terms of manipulating the value.
Just a thought - make sure that you fully understand what the input actually is. It's relatively unusual to give an input time in milliseconds; I'd expect that such an input would actually be a duration. This admittedly could be the number of milliseconds past midnight, but do be sure that it's not the number of milliseconds past some other arbitrary starting point.