I have a javascript function that I am creating. It is take a time (mm:ss) inputted by a user and then being displayed on a php page.
One of the problems that I, for some reason, cannot seem to work through is getting the minutes to increase by x when seconds is greater than 60.
For example, if the user enters in a run time of 01:53 and a penalty time of 00:25, the output is 01:18, when it really should be 02:18.
I created an if statement with the condition being if seconds is great than 60, to increment the minute by 1.
This is the function that I have so far. Also, is it easier to handle the input of time this way, or would it be easier and more efficient to handle time using the time() function?
function dfbcalc() {
var dfbrun = document.getElementById("dfb_run").value;
var dfbpen = document.getElementById("dfb_pen").value;
var splitdfbrun = dfbrun.split(':');
var splitdfbpen = dfbpen.split(':');
var dfbmin;
var dfbsec;
var dfbtot;
<!-- DFB Time input -->
dfbmin = parseInt(splitdfbrun[0]) + parseInt(splitdfbpen[0])
dfbmin = dfbmin % 60;
dfbsec = parseInt(splitdfbrun[1]) + parseInt(splitdfbpen[1])
dfbsec = dfbsec % 60;
if (dfbsec < 10) {
dfbsec = '0' + dfbsec;
}
if (dfbsec > 60) {
dfbmin = dfbmin + 1;
}
alert(+dfbmin + ':' + dfbsec)
dfbtot = '0' + dfbmin + ':' + dfbsec;
document.getElementById("dfb_com").value = dfbtot;
}
var dfbrun = "01:53"
var dfbpen = "00:25"
var splitdfbrun = dfbrun.split(':');
var splitdfbpen = dfbpen.split(':');
var dfbmin;
var dfbsec;
var dfbtot;
<!-- DFB Time input -->
dfbmin = parseInt(splitdfbrun[0]) + parseInt(splitdfbpen[0])
dfbmin = dfbmin % 60;
dfbsec = parseInt(splitdfbrun[1]) + parseInt(splitdfbpen[1])
dfbsec = dfbsec % 60;
if (dfbsec < 10) {
dfbsec = '0' + dfbsec;
}
if (dfbsec > 60) {
dfbmin = dfbmin + 1;
}
document.write(+dfbmin + ':' + dfbsec+"<br/>")
dfbtot = '0' + dfbmin + ':' + dfbsec;
document.write(dfbtot);
It's your order of operation. You're cutting the value off before checking how many minutes to add.
dfbmin = parseInt(splitdfbrun[0]) + parseInt(splitdfbpen[0])
dfbmin = dfbmin % 60;
dfbsec = parseInt(splitdfbrun[1]) + parseInt(splitdfbpen[1])
// Update Minutes
if (dfbsec >= 60) {
dfbmin = dfbmin + 1;
}
// Update seconds
dfbsec = dfbsec % 60;
if (dfbsec < 10) {
dfbsec = '0' + dfbsec;
}
You can convert all to seconds and back to minutes and seconds.
That makes it easier to grasp the order.
function pad(num) {
return ("0"+num).slice(-2);
}
function getTot(dfbrun,dfbpen) {
var splitdfbrun = dfbrun.split(':');
var splitdfbpen = dfbpen.split(':');
var dfbmin;
var dfbsec;
<!-- DFB Time input -->
dfbmin = parseInt(splitdfbrun[0],10) + parseInt(splitdfbpen[0],10);
dfbsec = dfbmin * 60;
dfbsec += parseInt(splitdfbrun[1],10) + parseInt(splitdfbpen[1],10);
dfbmin = Math.floor(dfbsec / 60);
dfbsec = dfbsec - dfbmin * 60;
return pad(dfbmin) + ':' + pad(dfbsec);
}
var dfbrun = "01:53"
var dfbpen = "00:25"
document.write(getTot(dfbrun,dfbpen));
Related
I just can't figure why this doesn't work for some odd values.
For example when trying to convert 22.68 to hours and minutes the output is 22:40.800000000000004 (Seconds shouldn't even appear)
if (str_HR_PER_WEEK.indexOf('.') > -1)
{
var str_HR_PER_WEEK_hrs = str_HR_PER_WEEK.substring(0 , str_HR_PER_WEEK.indexOf('.'));
var str_HR_PER_WEEK_mins = str_HR_PER_WEEK.substring(str_HR_PER_WEEK.indexOf('.') + 1);
var float_HR_PER_WEEK_mins = parseFloat("0." + (str_HR_PER_WEEK_mins), 10);
var float_HR_PER_WEEK_mins_actual = float_HR_PER_WEEK_mins * 60;
float_HR_PER_WEEK_mins_actual = float_HR_PER_WEEK_mins_actual.toString();
tables.CURRENT_EMPLOYEES.HOURS_PER_WEEK.value = getTwoDigitTime(str_HR_PER_WEEK_hrs) + ":" + getTwoDigitTime(float_HR_PER_WEEK_mins_actual);
}
else
{
tables.CURRENT_EMPLOYEES.HOURS_PER_WEEK.value = str_HR_PER_WEEK;
}
You have to ways to achieve that,
one, do the calculations yourself:
var decimalTimeString = "1.6578";
var decimalTime = parseFloat(decimalTimeString);
decimalTime = decimalTime * 60 * 60;
var hours = Math.floor((decimalTime / (60 * 60)));
decimalTime = decimalTime - (hours * 60 * 60);
var minutes = Math.floor((decimalTime / 60));
decimalTime = decimalTime - (minutes * 60);
var seconds = Math.round(decimalTime);
if(hours < 10)
{
hours = "0" + hours;
}
if(minutes < 10)
{
minutes = "0" + minutes;
}
if(seconds < 10)
{
seconds = "0" + seconds;
}
alert("" + hours + ":" + minutes + ":" + seconds);
Two, use built in function to convert to string and then to hh:mm:
var decimalTimeString = "1.6578";
var n = new Date(0,0);
n.setSeconds(+decimalTimeString * 60 * 60);
n.setMinutes(+decimalTimeString * 60);
var result = n.toTimeString().slice(0, 5);
document.write(result);
I've got a neat function to do just that:
function hoursToHHMM(hours) {
var h = String(Math.trunc(hours)).padStart(2, '0');
var m = String(Math.abs(Math.round((hours - h) * 60))).padStart(2, '0');
return h + ':' + m;
}
It handles negative values as a bonus.
Usage is trivial:
var hours = -7.33333;
console.log(hoursToHHMM(hours));
Results in: -07:20
You can play with it here: https://jsfiddle.net/r150c2me/
I Have a multiple text-box bounded with asp:repeater , I want to calculate time taken to type a text in that textbox, after textbox appeares. I tried it through onblur and onfocus events , but not working properly, as its taking onfocus time of next textbox while calculating time for previous textbox.
My repeater code is as follows
<asp:Repeater ID="rpt1" runat="server">
<asp:TextBox ID="txt1" autocomplete="off" EnableViewState="true" onfocus="f1(this);" onblur="f2(this);" runat="server"></asp:TextBox>
</asp:Repeater>
And javascript code like this
function f1(e) {
var onFocusTextTime = new Date().getTime();
alert("onFocus:" + onFocusTextTime);
return onFocusTextTime;
}
function f2(e) {
var responceForAnswer = "";
var diff_result = "";
var onBlurTextTime = new Date().getTime();
alert("onBlur:" + onBlurTextTime);
var onFocusTextTime = f1();
diff_result = onFocusTextTime - onBlurTextTime;
alert("diff_result:" + diff_result);
var hours = diff_result / (1000 * 60 * 60);
var absoluteHours = Math.floor(hours);
var h = absoluteHours > 9 ? absoluteHours : '0' + absoluteHours;
var minutes = (hours - absoluteHours) * 60;
var absoluteMinutes = Math.floor(minutes);
var m = absoluteMinutes > 9 ? absoluteMinutes : '0' + absoluteMinutes;
var seconds = (minutes - absoluteMinutes) * 60;
var absoluteSeconds = Math.floor(seconds);
var s = absoluteSeconds > 9 ? absoluteSeconds : '0' + absoluteSeconds;
responceForAnswer = h + ':' + m + ':' + s;
alert("responceForAnswer:" + responceForAnswer);
}
i just changed my code in following way and its worked
var onFocusTextTime;
var onFocusTextTimeForPrevTextBox = new Date().getTime();
function f1(e) {
onFocusTextTime = onFocusTextTimeForPrevTextBox;
onFocusTextTimeForPrevTextBox = new Date().getTime();
return onFocusTextTime;
}
function f2() {
var responceForAnswer = "";
var diff_result = "";
var onBlurTextTime = new Date().getTime();
var onFocusTextTime = f1();
diff_result = onBlurTextTime - onFocusTextTime;
var hours = diff_result / (1000 * 60 * 60);
var absoluteHours = Math.floor(hours);
var h = absoluteHours > 9 ? absoluteHours : '0' + absoluteHours;
var minutes = (hours - absoluteHours) * 60;
var absoluteMinutes = Math.floor(minutes);
var m = absoluteMinutes > 9 ? absoluteMinutes : '0' + absoluteMinutes;
var seconds = (minutes - absoluteMinutes) * 60;
var absoluteSeconds = Math.floor(seconds);
var s = absoluteSeconds > 9 ? absoluteSeconds : '0' + absoluteSeconds;
responceForAnswer = h + ':' + m + ':' + s;
console.log(responceForAnswer);
}
var currentTime = audio.currentTime | 0;
var duration = audio.duration | 0;
it works but,
it shows the audio's total length and current time in only second format
i want to convert the default second value in Minute:Second format
Try this (lightly tested):
var seconds = currentTime % 60;
var foo = currentTime - seconds;
var minutes = foo / 60;
if(seconds < 10){
seconds = "0" + seconds.toString();
}
var fixedCurrentTime = minutes + ":" + seconds;
var currentTime = audio.currentTime | 0;
var duration = audio.duration | 0;
var minutes = "0" + Math.floor(duration / 60);
var seconds = "0" + (duration - minutes * 60);
var dur = minutes.substr(-2) + ":" + seconds.substr(-2);
var minutes = "0" + Math.floor(currentTime / 60);
var seconds = "0" + (currentTime - minutes * 60);
var cur = minutes.substr(-2) + ":" + seconds.substr(-2);
You can simply write the code yourself; it's not as if it's complicated or would ever change:
function pad(num, size) {
var s = num + '';
while (s.length < size) {
s = '0' + s;
}
return s;
}
function format_seconds(secs) {
return Math.floor(secs / 60) + ':' + (pad(secs % 60, 2));
}
dropping my own answer after 5 years and 9 months.
function() {
if(this.myAudio.readyState > 0) {
var currentTime = this.myAudio.currentTime;
var duration = this.myAudio.duration;
var seconds: any = Math.floor(duration % 60);
var foo = duration - seconds;
var min: any = foo / 60;
var minutes: any = Math.floor(min % 60);
var hours: any = Math.floor(min / 60);
if(seconds < 10){
seconds = "0" + seconds.toString();
}
if(hours > 0){
this.audioDuration = hours + ":" + minutes + ":" + seconds;
} else {
this.audioDuration = minutes + ":" + seconds;
}
}
}
I used typescript, hope this helps...
Hello i have a problem with this code. I have tried several ways but without a success to get zero before hours etc. Also I checked different topics but without a success.
var timestamp = (Date.now() + 1000 * 2 * 60 * 24 * 1) - Date.now();
timestamp /= 1000;
function component(x, v) {
return Math.floor(x / v);
}
/* last thing i tried but maybe it will help someone
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
};
*/
var $div = $('div');
setInterval(function () {
timestamp--;
var
hours = component(timestamp, 60 * 60),
minutes = component(timestamp, 60) % 60,
seconds = component(timestamp, 1) % 60;
$div.html(hours + ":" + minutes + ":" + seconds);
}, 1000);
DEMO : http://jsfiddle.net/5z7ahmze/1/
Thank you for your time.
You can check your variable and add a 0 before if needed :
var comp = component(timestamp, 60 * 60);
var hour = comp < 10 ? '0' + comp : comp;
You can create a function like this
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
and then
$div.html(pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2));
Maybe that is what you want. Right?
EDIT
Ok, the final answer.
var interval = setInterval(function () {
timestamp--;
function addZero (number) {
var zeroedNumber = (number < 10) ? 0 + "" + number : number;
return zeroedNumber;
}
var
hours = addZero(component(timestamp, 60 * 60)),
minutes = addZero(component(timestamp, 60) % 60),
seconds = addZero(component(timestamp, 1) % 60);
$div.html(hours + ":" + minutes + ":" + seconds);
//Below, i helped you with a "stop count" handler. (:
if(hours == 0 & minutes == 0 & seconds == 1){
clearInterval(interval);
}
}, 1000);
Dinamically dding zeroes to your counter if (hour or minute or second) is < 10.
I think your code is working, if you call the pad function on the numbers:
$div.html(hours.pad() + ":" + minutes.pad() + ":" + seconds.pad());
My javascriptcode is working fine when i put alert.I need to Display time in Counter Format(Second decreasing way). Please help me in resolving this issue
<script type="text/javascript">
window.onload = function () {
//alert("request>>>");
var count = 0;
var start_actual_time = document.getElementById("timerStartTime").value;
var end_actual_time = document.getElementById("timerEndTime").value;
start_actual_time = new Date(start_actual_time);
var start_actual_time1 = new Date(start_actual_time.getTime());
start_actual_time1 = new Date(start_actual_time1);
var end_actual_time1 = new Date(end_actual_time);
var hours =end_actual_time1.getHours()- start_actual_time1.getHours();
var minutes = end_actual_time1.getMinutes() - start_actual_time1.getMinutes();
var seconds = end_actual_time1.getSeconds()- start_actual_time1.getSeconds();
seconds = hours * 3600 + minutes * 60 + seconds;
//alert ("seconds >>." +seconds);
timer(seconds);
};
function timer(seconds) {
alert("calling timer");
var s1 = Number(seconds);
var hours = Math.floor(s1 / 3600);
var minutes = Math.floor(s1 % 3600 / 60);
var s = Math.floor(s1 % 3600 % 60);
//alert("sec1" + s);
display = document.querySelector('#time');
var formatted = ((hours < 10)?("0" + hours):hours) + ":" + ((minutes < 10)?("0" + minutes):minutes) + ":" + ((s < 10)?("0" + s):s)
display.textContent = formatted ;
seconds = seconds - 1;
timer(seconds);
}
</script>
The way your code is written creates a
too much recursion
exception for me.
Therefore I have avoided recursive invokes and used javascript setInterval:
var refreshIntervalId = setInterval(function(){ timer(); }, 1000);
When your seconds reach zero, timer is stopped:
if (seconds == -1){
clearInterval(refreshIntervalId);
Link to working fiddle: http://jsfiddle.net/3ggspruf/2/