I'm having problems changing a timecode into frames using JavaScript.
running at 30 fps we get
"00:00:01:00" = 30 frames
So far so good, only I check the number of seconds at it gets converted to this:
Firstly Not sure what's going on there?!
It's been pointed out that I had \f instead of \nf.
Secondly
The frames returned is incorrect.
1 minute should be 1800 seconds not 1800000
Bonus points if you can tell me if that's an Ankh or not.
// set the frame rate or Frame Rat as we like to call him
var frameRat = 30 // fps
var numOfFrames = 60 // animation frame count
var animTime = "00:01:00:00" // time code
var a = convertTimeToFrames(animTime, frameRat);
var result = animTime + " at " + frameRat + " fps\n = " + a + " frames.";
alert(result);
function convertTimeCodeToSeconds(timeString, framerate)
{
var timeArray = timeString.split(":");
var hours = timeArray[0] * 60 * 60;
var minutes = timeArray[1] * 60;
var seconds = timeArray[2];
var frames = timeArray[3]*(1/framerate);
var str = "h:" + hours + "\nm:" + minutes + "\ns:" + seconds + "\f:" + frames;
alert(str)
var totalTime = hours + minutes + seconds + frames;
//alert(timeString + " = " + totalTime)
return totalTime;
}
function convertTimeToFrames(timeString, framerate)
{
var secs = convertTimeCodeToSeconds(timeString, framerate);
return secs * framerate;
}
You are doing operations between numbers and chars. Convert all values to Numbers after the split as follows:
// set the frame rate or Frame Rat as we like to call him
var frameRat = 30 // fps
var numOfFrames = 60 // animation frame count
var animTime = "00:01:00:00" // time code
var a = convertTimeToFrames(animTime, frameRat);
var result = animTime + " at " + frameRat + " fps\n = " + a + " frames.";
alert(result);
function convertTimeCodeToSeconds(timeString, framerate)
{
var timeArray = timeString.split(":");
var hours = parseInt(timeArray[0]) * 60 * 60;
var minutes = parseInt(timeArray[1]) * 60;
var seconds = parseInt(timeArray[2]);
var frames = parseInt(timeArray[3])*(1/framerate);
var str = "h:" + hours + "\nm:" + minutes + "\ns:" + seconds + "\nf:" + frames;
alert(str)
var totalTime = hours + minutes + seconds + frames;
//alert(timeString + " = " + totalTime)
return totalTime;
}
function convertTimeToFrames(timeString, framerate)
{
var secs = convertTimeCodeToSeconds(timeString, framerate);
return secs * framerate;
}
Related
how can I sum hours:minutes:seconds in JavaScript.
I mean by
04:32:05
03:14:03
To get
07:46:08
Ive tried
var time1 = "01:00:01";
var time2 = "01:00:10";
var time3 = "01:54:00";
var time4 = "01:30:00";
var time5 = "01:00:00";
var time6 = "01:00:00";
var time7 = "01:00:00";
var hour=0;
var minute=0;
var second=0;
var splitTime1= time1.split(':');
var splitTime2= time2.split(':');
var splitTime3= time3.split(':');
var splitTime4= time4.split(':');
var splitTime5= time5.split(':');
var splitTime6= time6.split(':');
var splitTime7= time7.split(':');
hour = parseInt(splitTime1[0]) + parseInt(splitTime2[0]) + parseInt(splitTime3[0]) + parseInt(splitTime4[0]) + parseInt(splitTime5[0]) + parseInt(splitTime6[0]) + parseInt(splitTime7[0])
minute = parseInt(splitTime1[1]) + parseInt(splitTime2[1]) + parseInt(splitTime3[1]) + parseInt(splitTime4[1]) + parseInt(splitTime5[1]) + parseInt(splitTime6[1]) + parseInt(splitTime7[1])
hour = hour + minute/60;
minute = minute%60;
second = parseInt(splitTime1[2]) + parseInt(splitTime2[2]) + parseInt(splitTime3[2])
+ parseInt(splitTime4[2]) + parseInt(splitTime5[2]) + parseInt(splitTime6[2]) +
parseInt(splitTime7[2])
minute = minute + second/60;
second = second%60;
console.log(hour+ ":" + minute + ":"+ second)
The output I get is 8.4:24.183333333333334:11 instad of 08:24:11
any suggestions?
your making it very complex, you can reduce this by converting into Date objects and then add each date to get the sum of all dates
Understanding Date and Time in JavaScript
The problem with your code is you are including the decimal point
hour = hour + minute/60;
you need to floor it.
hour = hour + Math.floor(minute/60);
Now how to do it without a lot of repetitive code.
function toSeconds(s) {
const parts = s.split(':');
return +parts[0] * 3600 + +parts[1] * 60 + +parts[2];
}
function secondsToHHMMSS(secs) {
return Math.floor(secs / 3600).toString().padStart(2, '0') + ':' +
(Math.floor(secs / 60) % 60).toString().padStart(2, '0') + ':' +
(secs % 60).toString().padStart(2, '0');
}
const timestamps = ["01:00:01", "01:00:10", "01:54:00", "01:30:00", "01:00:00", "01:00:00", "01:00:00"];
const totalSeconds = timestamps.reduce(function(total, ts) {
return total + toSeconds(ts);
}, 0);
const result = secondsToHHMMSS(totalSeconds);
console.log(result);
If you want to sum of times then you should try this
var addTime = function (time1, time2) {
// convert to ms
var dateObject1 = new Date(time1).valueOf();
var dateObject2 = new Date(time2).valueOf();
return dateObject1 + dateObject2;
}
var time1 = new Date().setHours(4, 32, 5, 0);
var time2 = new Date().setHours(3, 14, 3, 0);
var sum = new Date(addTime(time1, time2));
var getFormatedTime = function (time) {
return time.getHours()+':'+time.getMinutes()+':'+time.getSeconds()
}
console.log(getFormatedTime(sum))
The first thing you should look into is using an Array, since you have a number of objects of the same kind.
You should ideally have something like,
const times = ["04:32:05", "03:14:03", ...]
Once you have that, this problem reduces to a classic use-case for the reduce function.
The reduce function operates on an array and accumulates the value of the operation every step to yield one value at the end.
Here's an example solution for your problem
const times = ["04:32:05", "03:14:03"]
//const times = ["01:00:01", "01:00:10","01:54:00","01:30:00"]
let finalSum = times.reduce((sum, curr) => {
//Obtain the current timestamp as an array of numbers
//[HRS, MINS, SECS]
let currTimeStamp = curr.split(":").map(token => parseInt(token));
//Add the current seconds to the total seconds so far
sum[2] += currTimeStamp[2];
//See how many minutes you got leftover as a result of that addition
const leftOverMins = Math.floor(sum[2] / 60);
//Mod by 60, to keep the seconds under 60
sum[2] %= 60;
//Add the leftover minutes to the sum operation for minutes
sum[1] += (currTimeStamp[1] + leftOverMins);
//Similar procedure as above
const leftOverHours = Math.floor(sum[1] / 60);
sum[1] %= 60;
sum[0] += (currTimeStamp[0] + leftOverHours);
sum[0] %= 24;
return sum
}, [0, 0, 0])
console.log(finalSum.join(":"))
Hello hope this answer will help you, I recommand to replace your bottom part (where you calculate) I do pretty much the same thing, but in the good order and with round to avoid decimals problems
var time1 = "01:00:01";
var time2 = "01:00:10";
var time3 = "01:54:00";
var time4 = "01:30:00";
var time5 = "01:00:00";
var time6 = "01:00:00";
var time7 = "01:00:00";
var hour=0;
var minute=0;
var second=0;
var splitTime1= time1.split(':');
var splitTime2= time2.split(':');
var splitTime3= time3.split(':');
var splitTime4= time4.split(':');
var splitTime5= time5.split(':');
var splitTime6= time6.split(':');
var splitTime7= time7.split(':');
var allTimes = [splitTime1, splitTime2, splitTime3, splitTime4, splitTime5, splitTime6, splitTime7]
allTimes.forEach(element => {
hour += parseInt(element[0])
minute += parseInt(element[1])
second += parseInt(element[2])
})
minute += Math.round(second / 60);
second = second % 60;
hour += Math.round(minute / 60);
minute = minute % 60
console.log(hour+ ":" + minute + ":"+ second)
I have time (string) in this format: 01:01:01:01 (hours/minutes/seconds/milliseconds).
And I need to parse this time to milliseconds (number), how I can do this?
Use the following code ........
var string = "01:01:01:01";
var string_array = string.split(":");
var hours = string_array[0];
var mins = string_array[1];
var seconds = string_array[2];
var miliseconds = string_array[3];
var total_miliseconds = (hours*60*60*1000) + (mins*60*1000) + (seconds*1000) + (miliseconds);
console.log ("Total Miliseconds: " + total_miliseconds);
You can use something like this:
function toMilisecond(time) {
[hours, mins, seconds, miliseconds] = time.split(":");
return hours * 3600000 + mins * 60000 + seconds * 1000 + Number(miliseconds);
}
console.log(toMilisecond("01:01:01:01"));
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...
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));
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/