Decimal Degrees to Degrees Minutes And Seconds in Javascript - javascript

Im trying to write a function that takes my decimal degrees (lat or long) and converts them to DMS degrees minutes seconds. I know I am meant to times the decimal point number by 60 then it's decimal again. But am a noob. Would I split the number?
function ConvertDDToDMS(DD) {
eg. DD =-42.4
D= 42;
M= 4*60;
S= .M * 60;
var DMS =
return DMS //append Direction (N, S, E, W);
}
Am I on the right track?

function ConvertDDToDMS(D, lng) {
return {
dir: D < 0 ? (lng ? "W" : "S") : lng ? "E" : "N",
deg: 0 | (D < 0 ? (D = -D) : D),
min: 0 | (((D += 1e-9) % 1) * 60),
sec: (0 | (((D * 60) % 1) * 6000)) / 100,
};
}
The above gives you an object {deg, min, sec, dir} with sec truncated to two digits (e.g. 3.14) and dir being one of N, E, S, W depending on whether you set the lng (longitude) parameter to true. e.g.:
ConvertDDToDMS(-18.213, true) == {
deg : 18,
min : 12,
sec : 46.79,
dir : 'W'
}
Or if you just want the basic string:
function ConvertDDToDMS(D){
return [0|D, 'd ', 0|(D=(D<0?-D:D)+1e-4)%1*60, "' ", 0|D*60%1*60, '"'].join('');
}
ConvertDDToDMS(-18.213) == `-18d 12' 47"`
[edit June 2019] -- fixing an 8 year old bug that would sometimes cause the result to be 1 minute off due to floating point math when converting an exact minute, e.g. ConvertDDToDMS(4 + 20/60).
[edit Dec 2021] -- Whoops. Fix #2. Went back to the original code and added 1e-9 to the value which a) bumps any slightly low floating point errors to the next highest number and b) is less than .01 sec, so has no effect on the output. Added 1e-4 to the "string" version which is the same fix, but also rounds seconds (it's close to 1/2 sec).

It's not clear how you need the output. Here's a version that returns all 3 values as a string:
function ConvertDDToDMS(dd)
{
var deg = dd | 0; // truncate dd to get degrees
var frac = Math.abs(dd - deg); // get fractional part
var min = (frac * 60) | 0; // multiply fraction by 60 and truncate
var sec = frac * 3600 - min * 60;
return deg + "d " + min + "' " + sec + "\"";
}

Update: I remove the part that did not make any sense (thanks cwolves!).
Here you have yet another implementation. It won't be as short nor efficient as the previous ones, but hopefully much easier to understand.
To get it right, first you need to understand how the calculations are done and only then attempt to implement them. For that, pseudocode is a great option, since you write down the steps in plain English or a simplified syntax that is easy to understand, and then translate it onto the programming language of choice.
I hope it's useful!
/* This is the pseudocode you need to follow:
* It's a modified version from
* http://en.wikipedia.org/wiki/Geographic_coordinate_conversion#Conversion_from_Decimal_Degree_to_DMS
function deg_to_dms ( degfloat )
Compute degrees, minutes and seconds:
deg ← integerpart ( degfloat )
minfloat ← 60 * ( degfloat - deg )
min ← integerpart ( minfloat )
secfloat ← 60 * ( minfloat - min )
Round seconds to desired accuracy:
secfloat ← round( secfloat, digits )
After rounding, the seconds might become 60. These two
if-tests are not necessary if no rounding is done.
if secfloat = 60
min ← min + 1
secfloat ← 0
end if
if min = 60
deg ← deg + 1
min ← 0
end if
Return output:
return ( deg, min, secfloat )
end function
*/
function deg_to_dms (deg) {
var d = Math.floor (deg);
var minfloat = (deg-d)*60;
var m = Math.floor(minfloat);
var secfloat = (minfloat-m)*60;
var s = Math.round(secfloat);
// After rounding, the seconds might become 60. These two
// if-tests are not necessary if no rounding is done.
if (s==60) {
m++;
s=0;
}
if (m==60) {
d++;
m=0;
}
return ("" + d + ":" + m + ":" + s);
}

Try this working perfect!!!
function truncate(n) {
return n > 0 ? Math.floor(n) : Math.ceil(n);
}
function getDMS(dd, longOrLat) {
let hemisphere = /^[WE]|(?:lon)/i.test(longOrLat)
? dd < 0
? "W"
: "E"
: dd < 0
? "S"
: "N";
const absDD = Math.abs(dd);
const degrees = truncate(absDD);
const minutes = truncate((absDD - degrees) * 60);
const seconds = ((absDD - degrees - minutes / 60) * Math.pow(60, 2)).toFixed(2);
let dmsArray = [degrees, minutes, seconds, hemisphere];
return `${dmsArray[0]}°${dmsArray[1]}'${dmsArray[2]}" ${dmsArray[3]}`;
}
var lat = 13.041107;
var lon = 80.233232;
var latDMS = getDMS(lat, 'lat');
var lonDMS = getDMS(lon, 'long');
console.log('latDMS: '+ latDMS);
console.log('lonDMS: '+ lonDMS);
Output:
latDMS: 13°2'27.99" N
lonDMS: 80°13'59.64" E

A solution with the option for specifying the decimal places in output seconds and correction of any edge cases due to rounding seconds and minutes.
// # input {deg} Numeric; degrees number to convert
// # input {dplaces} Decimal places to use for output seconds
// Default 0 places
// # return {DMS} string degrees (°) minutes (') seconds (")
//
function degToDMS (deg, dplaces=0) {
var d = Math.floor (deg); // make degrees
var m = Math.floor((deg-d)*60); // make minutes
var s = Math.round(((deg-d)*60-m)*60*Math.pow(10,dplaces))/Math.pow(10,dplaces); // Make sec rounded
s == 60 && (m++, s=0 ); // if seconds rounds to 60 then increment minutes, reset seconds
m == 60 && (d++, m=0 ); // if minutes rounds to 60 then increment degress, reset minutes
return (d + "° " + m + "' " + s+'"'); // create output DMS string
}
// ----- tests ------
console.log(degToDMS(55.23456)); // 55° 14' 4"
console.log(degToDMS(55.23456 ,3)); // 55° 14' 4.416"
console.log(degToDMS(4 + 20/60 ,2)); // 4° 20' 0"
console.log(degToDMS(89.64789 ,2)); // 89° 38' 52.4"
console.log(degToDMS(-23.1234567,3)); // -24° 52' 35.556"

This one works %100 in TypeScript:
ConvertDDToDMS(deg: number, lng: boolean): string {
var d = parseInt(deg.toString());
var minfloat = Math.abs((deg - d) * 60);
var m = Math.floor(minfloat);
var secfloat = (minfloat - m) * 60;
var s = Math.round((secfloat + Number.EPSILON) * 100) / 100
d = Math.abs(d);
if (s == 60) {
m++;
s = 0;
}
if (m == 60) {
d++;
m = 0;
}
let dms = {
dir: deg < 0 ? lng ? 'W' : 'S' : lng ? 'E' : 'N',
deg: d,
min: m,
sec: s
};
return `${dms.deg}\u00B0 ${dms.min}' ${dms.sec}" ${dms.dir}`
}

private static DecimalFormat DecimalFormat = new DecimalFormat(".##");
public static void main(String[] args){
double decimal_degrees = 22.4229541515;
System.out.println(getDMS(decimal_degrees));
}
public static String getDMS(double decimal_degrees) {
double degree = Math.floor(decimal_degrees);
double minutes = ((decimal_degrees - Math.floor(decimal_degrees)) * 60.0);
double seconds = (minutes - Math.floor(minutes)) * 60.0;
return ((int)degree)+":"+((int)minutes)+":"+decimalFormat.format(seconds);
}
INPUT : 22.4229541515 OUTPUT: 22:25:22.63

Based on above answer, i've written them into javascript and php style.
JS-
function convertDDToDMS(deg, lng){
var d = parseInt(deg);
var minfloat = Math.abs((deg-d) * 60);
var m = Math.floor(minfloat);
var secfloat = (minfloat-m)*60;
var s = Math.round(secfloat);
d = Math.abs(d);
if (s==60) {
m++;
s=0;
}
if (m==60) {
d++;
m=0;
}
return {
dir : deg<0?lng?'W':'S':lng?'E':'N',
deg : d,
min : m,
sec : s
};
}
PHP-
function convertDDtoDMS($deg, $lng){
$dd = intval($deg);
$minfloat = abs(($deg - $dd) * 60);
$mm = floor($minfloat);
$secfloat = ($minfloat - $mm) * 60;
$ss = round($secfloat);
$dd = abs($dd);
if($ss == 60){
$mm++;
$ss = 0;
}
if($mm == 60){
$dd++;
$mm = 0;
}
$dd = array(
'dir' => $deg < 0 ? ($lng ? 'W' : 'S') : ($lng ? 'E' : 'N'),
'deg' => abs($dd),
'min' => $mm,
'sec' => $ss,
);
return $dd;
}

couldnt get the script above working, after some time came up with this;
just give the dms to the script
function ConvertDMSToDEG(dms) {
var dms_Array = dms.split(/[^\d\w\.]+/);
var degrees = dms_Array[0];
var minutes = dms_Array[1];
var seconds = dms_Array[2];
var direction = dms_Array[3];
var deg = (Number(degrees) + Number(minutes)/60 + Number(seconds)/3600).toFixed(6);
if (direction == "S" || direction == "W") {
deg = deg * -1;
} // Don't do anything for N or E
return deg;
}
and visa versa just give the degrees to the script, and true of false for lat (latitude)
function ConvertDEGToDMS(deg, lat) {
var absolute = Math.abs(deg);
var degrees = Math.floor(absolute);
var minutesNotTruncated = (absolute - degrees) * 60;
var minutes = Math.floor(minutesNotTruncated);
var seconds = ((minutesNotTruncated - minutes) * 60).toFixed(2);
if (lat) {
var direction = deg >= 0 ? "N" : "S";
} else {
var direction = deg >= 0 ? "E" : "W";
}
return degrees + "°" + minutes + "'" + seconds + "\"" + direction;
}
hope this helps people..

I'm surprised all solutions are using some additional logic to handle the "rounds to 60" cases (if they're aware of it at all), but nobody thought of doing it the other way round, starting with (rounded) seconds and then using mod and int-div and not have to worry about all that:
function coordToStr(coord)
{
let seconds = Math.round(Math.abs(coord) * 3600)
let sec = Math.floor(seconds % 60)
let minutes = Math.floor(seconds / 60)
let min = minutes % 60
let deg = Math.floor(minutes / 60)
return deg + "°" + ((min < 10) ? "0" : "") + min + "'" + ((sec < 10) ? "0" : "") + sec
}
Sorry, this is without the N/S, E/W part, would need some additional method calling it.
If you want second-fractions, you could use this:
function coordToStrWithDecimals(coord)
{
let centiSecs = Math.round(Math.abs(coord) * 360000)
let frac = Math.floor(centiSecs % 100)
let seconds = Math.floor(centiSecs / 100)
let sec = Math.floor(seconds % 60)
let minutes = Math.floor(seconds / 60)
let min = minutes % 60
let deg = Math.floor(minutes / 60)
return deg + "°" + ((min < 10) ? "0" : "") + min + "'" + ((sec < 10) ? "0" : "") + sec + "." + ((frac < 10) ? "0" : "") + frac + '"'
}

just for remark, the answer
function ConvertDDToDMS(D){
return [0|D, 'd ', 0|(D<0?D=-D:D)%1*60, "' ", 0|D*60%1*60, '"'].join('');
}
does not work for angles between -1° and 0°.
Bad luck!
hc

Related

How to add milliseconds with JavaScript

I'm adding two time strings in order to get single time string in h:m:s format with milliseconds to be added as well.
I only managed until the seconds. Now I also want to add the milliseconds
I also want to add more than two time strings, that is, more than two parameters
function addTimes (startTime, endTime) {
var times = [ 0, 0, 0 ]
var max = times.length
var a = (startTime || '').split(':')
var b = (endTime || '').split(':')
// normalize time values
for (var i = 0; i < max; i++) {
a[i] = isNaN(parseInt(a[i])) ? 0 : parseInt(a[i])
b[i] = isNaN(parseInt(b[i])) ? 0 : parseInt(b[i])
}
// store time values
for (var i = 0; i < max; i++) {
times[i] = a[i] + b[i]
}
var hours = times[0]
var minutes = times[1]
var seconds = times[2]
if (seconds >= 60) {
var m = (seconds / 60) << 0
minutes += m
seconds -= 60 * m
}
if (minutes >= 60) {
var h = (minutes / 60) << 0
hours += h
minutes -= 60 * h
}
return ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2)
}
alert(addTimes('9:10:10', '1:0:0'));
You can just extend your existing function to handle milliseconds the same way it's currently handling minutes and seconds, just with a factor of 1,000 instead of 60.
function addTimes(startTime, endTime) {
// Add element for milliseconds to times
var times = [0, 0, 0, 0];
var max = times.length;
// Split on ':' and '.'
var a = (startTime || '').split(/[:.]/);
var b = (endTime || '').split(/[:.]/);
// normalize time values
for (var i = 0; i < max; i++) {
a[i] = isNaN(parseInt(a[i])) ? 0 : parseInt(a[i]);
b[i] = isNaN(parseInt(b[i])) ? 0 : parseInt(b[i]);
}
// store time values
for (var i = 0; i < max; i++) {
times[i] = a[i] + b[i];
}
var hours = times[0];
var minutes = times[1];
var seconds = times[2];
// Add variable for milliseconds
var milliseconds = times[3];
// Add milliseconds first
if (milliseconds >= 1000) {
var s = (milliseconds / 1000) << 0;
seconds += s;
milliseconds -= 1000 * s;
}
// Continue as for original
if (seconds >= 60) {
var m = (seconds / 60) << 0;
minutes += m;
seconds -= 60 * m;
}
if (minutes >= 60) {
var h = (minutes / 60) << 0;
hours += h;
minutes -= 60 * h;
}
return ('0' + hours).slice(-2) + ':' +
('0' + minutes).slice(-2) + ':' +
('0' + seconds).slice(-2) + '.' +
// Add milliseconds to return
('00' + milliseconds).slice(-3);
}
console.log(addTimes('9:10:10.500', '1:0:55.501'));
I'd simplify things a bit and do away with the if statements and just do the overflow thing every time. Also, it can be made a bit more tolerant of partial strings and invalid values, e.g.
/* Add times in format h:mm:ss.sss
** return total time in same format
** Accepts partial times, first digit treated as hours
** e.g. 1:30 is equivalent to 1:30:00.000
** Invalid and missing parts treated as 0 (zero)
** #param {string} timeA : time in format h:mm:ss.sss
** #param {string} timeB : time in format h:mm:ss.sss
** #returns {string} sum of timeA and timeB in format h:mm:ss.sss
*/
function addTime(timeA, timeB) {
// Pad function for numbers less than 100
let z = (n, len) => String(n).padStart(len, '0');
// Split timestamps into parts as numbers, NaN => 0
let parts = s => String(s).split(/[:.]/).map(v => Number(v) || 0);
let [aH, aM, aS, aMs] = parts(timeA);
let [bH, bM, bS, bMs] = parts(timeB);
// Initialise total values, tx is just a placeholder
let tH = 0, tM = 0, tS = 0, tMs = 0, tX;
// Helper to add time values - omitting f just adds a + b
let factorSum = (a, b, f) => {
let t = (a || 0) + (b || 0);
return f? [t / f | 0, t % f] : [0, t];
};
// Add parts in order using helper
[tS, tMs] = factorSum(aMs, bMs, 1000);
[tM, tS] = factorSum(aS, bS + tS, 60);
[tH, tM] = factorSum(aM, bM + tM, 60);
[tX, tH] = factorSum(aH, bH + tH);
// Return formatted result
return tH + ':' + z(tM,2) + ':' + z(tS,2) + '.' + z(tMs,3);
}
// No overflow
console.log(addTime('1:01:01.001', '2:02:02.200')); // 3:03:03.201
// Overflow mins, secs, milliseconds
console.log(addTime('1:59:59.999', '0:00:00.001')); // 2:00:00.000
// Partial times
console.log(addTime('1', '2:30:1')); // 3:30:01.000
// Missing parameter treated as 0
console.log(addTime()); // 0:00:00.000
console.log(addTime('1:15')); // 1:15:00.000
// Invalid parts treated as zero
console.log(addTime('1:15', 'foo')); // 1:15:00.000
console.log(addTime('1:15', '2:blah:23.001')); // 3:15:23.001
If I could use a addition assignment += with destructuring it could be simpler, but that's not possible yet. :-)

How to inverse this command?

I'm making a voice ranking system on Discord, for now everything is good, but I'm stuck in something.
I made this command, it will let you able to get the Xp amount from a level, and it converts the Xp to time, like:
The levels system is based on that code:
module.exports = {
name: 'lvl',
execute(message, args) {
const start = 216000 //From lvl1 to lvl2
const coe = 125 //Coefficient
const lvls = 2 //The number of lvls in each stage
function msToTime(duration) { //ms to time, usage: msToTime(xp + '00'), 1 xp = 0.1 sec
var milliseconds = parseInt((duration % 1000) / 100),
seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
hours = Math.floor((duration / (1000 * 60 * 60)));
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + "h & " + minutes + "m & " + seconds + "s";
}
function Stage(lvl) { //Get the stage number from a lvl, stages are used to divide lvls
const a = + lvl - 1
const b = Math.floor((a/2) + 1)
const c = b.toString()
console.log(c)
return c;
}
function Percentage(stage) { //Get the lvls inflation percentage, each stage the percentage will be larger
const a = stage * coe
const b = a.toString()
console.log(b)
return b;
}
function Xp(lvl) { //Get the Xp amount from a lvl
const a = (Percentage(Stage(lvl)) * (lvl - 1))
const b = start + (start * a / 100)
const c = b.toString()
console.log(c)
return c;
}
message.channel.send('Xp: ' + Xp(args[0]) + ', Time: ' + msToTime(Xp(args[0]) + '00')) //Send the Xp amount and the time
}
};
But now, I don't know how to inverse it to get the level from Xp, for example:
You type .xp 486000, it will give you lvl 2.
So anyone has an idea? ~and thx.
Edit: (I removed the stages function)
module.exports = {
name: 'lvl',
execute(message, args) {
const start = 216000 //From lvl1 to lvl2
const coe = 65 //Coefficient
function msToTime(duration) { //ms to time, usage: msToTime(xp + '00'), 1 xp = 0.1 sec
var milliseconds = parseInt((duration % 1000) / 100),
seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
hours = Math.floor((duration / (1000 * 60 * 60)));
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + "h & " + minutes + "m & " + seconds + "s";
}
function Percentage(lvl) { //Get the lvls inflation percentage
const a = lvl * coe
const b = a.toString()
console.log(b)
return b;
}
function Xp(lvl) { //Get the Xp amount from a lvl
const a = (Percentage(lvl) * (lvl - 1))
const b = start + (start * a / 100)
const c = b.toString()
console.log(c)
return c;
}
message.channel.send('Xp: ' + Xp(args[0]) + ', Time: ' + msToTime(Xp(args[0]) + '00')) //Send the Xp amount and the time
}
};
I am attempting this question because I am curious how the algebraic equations will be. So first of all it will NOT work because Math.floor is a lossy function. If lvl is even the answer is correct otherwise it MAY not be.
So, let's make some algebraic equations.
stage = st, coe = c, lvl = l, start = s, percentage = p, xp = x
stage is:
Percentage is:
XP is:
Solving for Lvl:
So, you can probably make one JS function out of this, but it will not be correct as I said, Math.floor is a lossy function.
You can check it out here:
https://www.mathpapa.com/algebra-calculator.html?q=x%3Ds%5Cleft(1%2B%5Cfrac%7B%5Cleft%5B%5Cleft(%5Cfrac%7B%5Cleft(l-1%5Cright)%7D%7B2%7D%2B1%5Cright)c%5Cleft(l-1%5Cright)%5Cright%5D%7D%7B100%7D%5Cright)
EDIT:
Without the Stage function, the equation will be:
You could define an object with the specific threshold at which a user levels up. Maybe level 1 is 0 seconds, level 2 is 1 hour, level 3 is 5 hours etc, you'd do it like this:
let levels = {
"1": 0
"2": 1 * 60 * 60 * 1000
"3": 5 * 60 * 60 * 1000
}
Then you could loop through the user's input and check whether it's higher than each level's threshold. Currently your XP is their time in ms. So if they say 10800000 XP (3 hours), a function like this would return that their level is "2":
function checkLevel(xp) => {
let outLevel = "0";
for (level in levels) {
let levelXp = levels[level];
if (xp > levelXp) outLevel = level;
else break;
}
return outLevel;
}
If you don't want level XP hardcoded, you could make use of a function that takes in a level and returns the XP required using whatever algorithm you want (ie, your Xp() function). You could do something like this:
function checkLevel(xp) => {
let checkLevel = 0;
let outLevel = 0;
while (true) {
let levelXp = levelToXp(checkLevel);
if (xp > levelXp) outLevel = level;
else return outLevel.toString();
checkLevel++;
}
}
This is it:
function Lvl(xp) { //Get the lvl from Xp
const a = ((start*coe) + Math.sqrt((start*coe)*((start*coe)-(400*start)+(400*xp)))) / (2*start*coe)
const b = a.toString()
console.log(a)
return b;
}

Convert seconds to days, hours, minutes and seconds

I have a Javascript timing event with an infinite loop with a stop button.
It will display numbers when start button is click.Now I want this numbers converted to something like 4 hours, 3 minutes , 50 seconds
var c = 0;
var t;
var timer_is_on = 0;
function timedCount() {
document.getElementById('txt').value = c;
c = c + 1;
t = setTimeout(function() {
timedCount()
}, 1000);
}
function doTimer() {
if (!timer_is_on) {
timer_is_on = 1;
timedCount();
}
}
function stopCount() {
clearTimeout(t);
timer_is_on = 0;
}
$(".start").on("click", function() {
//var start = $.now();
//alert(start);
//console.log(start);
doTimer();
$(".end").show();
$(".hide_div").show();
});
$(".end").on("click", function() {
stopCount();
});
.hide_div {
display: none;
}
.end {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="start">Start</p>
<p class="end">End</p>
<p class="hide_div">
<input type="text" id="txt" />//display numbers eg 12345
</p>
How to convert numbers like 123456 to 1 day, 4 hours, 40 min, 45 seconds?
I suggest doing this way!:
function secondsToDhms(seconds) {
seconds = Number(seconds);
var d = Math.floor(seconds / (3600*24));
var h = Math.floor(seconds % (3600*24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);
var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : "";
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return dDisplay + hDisplay + mDisplay + sDisplay;
}
Use Math like this way, Second param in parseInt is for base, which is optional
var seconds = parseInt(123456, 10);
var days = Math.floor(seconds / (3600*24));
seconds -= days*3600*24;
var hrs = Math.floor(seconds / 3600);
seconds -= hrs*3600;
var mnts = Math.floor(seconds / 60);
seconds -= mnts*60;
console.log(days+" days, "+hrs+" Hrs, "+mnts+" Minutes, "+seconds+" Seconds");
Your given seconds 123456 would be 1 days, 10 Hrs, 17 Minutes, 36 Seconds not 1 days, 4 Hrs, 40 Minutes, 45 Seconds
function countdown(s) {
const d = Math.floor(s / (3600 * 24));
s -= d * 3600 * 24;
const h = Math.floor(s / 3600);
s -= h * 3600;
const m = Math.floor(s / 60);
s -= m * 60;
const tmp = [];
(d) && tmp.push(d + 'd');
(d || h) && tmp.push(h + 'h');
(d || h || m) && tmp.push(m + 'm');
tmp.push(s + 's');
return tmp.join(' ');
}
// countdown(3546544) -> 41d 1h 9m 4s
// countdown(436654) -> 5d 1h 17m 34s
// countdown(3601) -> 1h 0m 1s
// countdown(121) -> 2m 1s
My solution with map() and reduce():
const intervalToLevels = (interval, levels) => {
const cbFun = (d, c) => {
let bb = d[1] % c[0],
aa = (d[1] - bb) / c[0];
aa = aa > 0 ? aa + c[1] : '';
return [d[0] + aa, bb];
};
let rslt = levels.scale.map((d, i, a) => a.slice(i).reduce((d, c) => d * c))
.map((d, i) => ([d, levels.units[i]]))
.reduce(cbFun, ['', interval]);
return rslt[0];
};
const TimeLevels = {
scale: [24, 60, 60, 1],
units: ['d ', 'h ', 'm ', 's ']
};
const secondsToString = interval => intervalToLevels(interval, TimeLevels);
If you call secondsToString(123456), you can get "1d 10h 17m 36s "
Here is my solution, a simple function that will round to the nearest second!
var returnElapsedTime = function(epoch) {
//We are assuming that the epoch is in seconds
var hours = epoch / 3600,
minutes = (hours % 1) * 60,
seconds = (minutes % 1) * 60;
return Math.floor(hours) + " hours, " + Math.floor(minutes) + " minutes, " + Math.round(seconds) + " seconds";
}
Came up with my own variation to some of the solutions suggested in this thread.
if (!Number.prototype.secondsToDHM) {
Number.prototype.secondsToDHM = function() {
const secsPerDay = 86400;
const secsPerHour = 3600;
const secsPerMinute = 60;
var seconds = Math.abs(this);
var minus = (this < 0) ? '-' : '';
var days = Math.floor(seconds / secsPerDay);
seconds = (seconds % secsPerDay);
var hours = Math.floor(seconds / secsPerHour);
seconds = (seconds % secsPerHour);
var minutes = Math.floor(seconds / secsPerMinute);
seconds = (seconds % secsPerMinute);
var sDays = new String(days).padStart(1, '0');
var sHours = new String(hours).padStart(2, '0');
var sMinutes = new String(minutes).padStart(2, '0');
return `${minus}${sDays}D ${sHours}:${sMinutes}`;
}
}
var a = new Number(50000).secondsToDHM();
var b = new Number(100000).secondsToDHM();
var c = new Number(200000).secondsToDHM();
var d = new Number(400000).secondsToDHM();
console.log(a);
console.log(b);
console.log(c);
console.log(d);
This answer builds upon on Andris' approach to this question, but it doesn't have trailing commas if lesser units are not present.
It also borrows from this answer dealing with joining array values only if truthy:
https://stackoverflow.com/a/19903063
I'm not a javascript god and it's probably horribly over-engineered, but hopefully readable and correct!
function sformat(s) {
// create array of day, hour, minute and second values
var fm = [
Math.floor(s / (3600 * 24)),
Math.floor(s % (3600 * 24) / 3600),
Math.floor(s % 3600 / 60),
Math.floor(s % 60)
];
// map over array
return $.map(fm, function(v, i) {
// if a truthy value
if (Boolean(v)) {
// add the relevant value suffix
if (i === 0) {
v = plural(v, "day");
} else if (i === 1) {
v = plural(v, "hour");
} else if (i === 2) {
v = plural(v, "minute");
} else if (i === 3) {
v = plural(v, "second");
}
return v;
}
}).join(', ');
}
function plural(value, unit) {
if (value === 1) {
return value + " " + unit;
} else if (value > 1) {
return value + " " + unit + "s";
}
}
console.log(sformat(60)); // 1 minute
console.log(sformat(3600)); // 1 hour
console.log(sformat(86400)); // 1 day
console.log(sformat(8991)); // 2 hours, 29 minutes, 51 seconds
If you needed to convey the duration more 'casually' in words, you could also do something like:
var remaining_duration = sformat(117);
// if a value is returned, add some prefix and suffix
if (remaining_duration !== "") {
remaining_duration = "about " + remaining_duration + " left";
}
$(".remaining_duration").text(remaining_duration);
// returns 'about 1 minute, 57 seconds left'
I further tweaked the code by Svetoslav as follows:
function convertSecondsToReadableString(seconds) {
seconds = seconds || 0;
seconds = Number(seconds);
seconds = Math.abs(seconds);
const d = Math.floor(seconds / (3600 * 24));
const h = Math.floor(seconds % (3600 * 24) / 3600);
const m = Math.floor(seconds % 3600 / 60);
const s = Math.floor(seconds % 60);
const parts = [];
if (d > 0) {
parts.push(d + ' day' + (d > 1 ? 's' : ''));
}
if (h > 0) {
parts.push(h + ' hour' + (h > 1 ? 's' : ''));
}
if (m > 0) {
parts.push(m + ' minute' + (m > 1 ? 's' : ''));
}
if (s > 0) {
parts.push(s + ' second' + (s > 1 ? 's' : ''));
}
return parts.join(', ');
}
Short answer:
var s = (Math.floor(123456/86400) + ":" + (new Date(123456 * 1000)).toISOString().substr(11, 8)).split(":");
console.log(`${s[0]} days, ${s[1]} hours, ${s[2]} minutes, ${s[3]} seconds` )
Edit:
Let me break it down in parts :
Math.floor(123456/86400)
86400 is the the total seconds in a day (60 seconds * 60 minutes * 24 hours). Dividing the inputted seconds by this value gives us number of days. We just need the whole part so we use Math.floor because the fractional piece is handled by this part:
(new Date(123456 * 1000)).toISOString().substr(11, 8)
the explanation can be found here:
Convert seconds to HH-MM-SS with JavaScript?
It just outputs hh:mm:ss, no days. So the first part and this part is a perfect combination
We concatenate using a colon (:) as a separator. The string looks like this:
'1:10:17:36'
We split it into an array with .split(":");. Then finally, we format the elements of the array for the desired output.
I've tweaked the code that Andris posted https://stackoverflow.com/users/3564943/andris
// https://stackoverflow.com/questions/36098913/convert-seconds-to-days-hours-minutes-and-seconds
function app_ste_36098913_countdown_seconds_to_hr(seconds) {
seconds = seconds || 0;
seconds = Number(seconds);
seconds = Math.abs(seconds);
var d = Math.floor(seconds / (3600*24));
var h = Math.floor(seconds % (3600*24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);
var parts = new Array();
if (d > 0) {
var dDisplay = d > 0 ? d + ' ' + (d == 1 ? "day" : "days") : "";
parts.push(dDisplay);
}
if (h > 0) {
var hDisplay = h > 0 ? h + ' ' + (h == 1 ? "hour" : "hours") : "";
parts.push(hDisplay)
}
if (m > 0) {
var mDisplay = m > 0 ? m + ' ' + (m == 1 ? "minute" : "minutes") : "";
parts.push(mDisplay)
}
if (s > 0) {
var sDisplay = s > 0 ? s + ' ' + (s == 1 ? "second" : "seconds") : "";
parts.push(sDisplay)
}
return parts.join(', ', parts);
}
You will probably find using epoch timestamps more straightforward: As detailed in Convert a Unix timestamp to time in JavaScript, the basic method is like so:
<script>
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date1 = new Date();
alert ('easy trick to waste a few seconds...' + date1);
// var date = date2 - date1;
// Hours part from the timestamp
var hours1 = date1.getHours();
// Minutes part from the timestamp
var minutes1 = "0" + date1.getMinutes();
// Seconds part from the timestamp
var seconds1 = "0" + date1.getSeconds();
var date2 = new Date();
// Hours part from the timestamp
var hours2 = date2.getHours();
// Minutes part from the timestamp
var minutes2 = "0" + date2.getMinutes();
// Seconds part from the timestamp
var seconds2 = "0" + date2.getSeconds();
// Will display time in 10:30:23 format
// var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
var elapsedHrs = hours2 - hours1;
var elapsedMin = minutes2.substr(-2) -minutes1.substr(-2);
var elapsedSec = seconds2.substr(-2) - seconds1.substr(-2);
var elapsedTime = elapsedHrs + ' hours, ' + elapsedMin + ' minutes, ' + elapsedSec + ' seconds';
alert ('time between timestamps: ' + elapsedTime);
</script>
Be warned that this script needs some work since for now it will give negative values for things like date1 = 12:00:00 and date2 = 12:00:05, but I'll leave that to you fo now.
You should rewrite your code to take a timestamp ( var x = new Date(); ) at the start of your timer and one whenever you are done/want to check elapsed time, and subtract the two before parsing out elapsed seconds, minutes, hours etc as required.
This is my take at the question, even if it is an old topic.
You can use a loop to compute everything for you :
function time_remaining(date1, date2) {
let seconds = (date2 - date1) / 1000
let units = ["years", "days", "h", "min", "s"]
let limit_units = [365, 24, 60, 60, 1]
const reducer = (accumulator, curr) => accumulator * curr;
let time = []
for (let i = 0; i < units.length; i++) {
let divisor = limit_units.slice(i).reduce(reducer)
let value = Math.floor(seconds / divisor)
seconds = seconds - value * divisor
time.push(value)
}
return clean_time(time, units)
}
// at this point, you have your answer. However,
// we can improve the result by removing all none
// significative null units (i.e, if your countdown is
// only about hours, minutes and seconds, it is not
// going to include years and days.)
function clean_time(time, units) {
time = time.reverse()
while (time[time.length - 1] == 0) {
time.pop()
}
return [time.reverse(), units.slice(-time.length)]
}
let date1 = Date.parse("2023-07-09T17:50:33")
console.log(time_remaining(Date.now(), date1))

Creating timecode from frames

I have a FPS (frames per second) of 30. I have a total FPS so far, lets say 1020. I want to display this as a formatted timecode, as below.
var fps = 30;
var currentFrame = 1020;
var resultString = ; // HH:MM:SS:FF
Are there any Javascript functions built in for formatting like this?
To be clear, I need the string to be formatted as such: HH:MM:SS:FF
Are you looking for a built-in JS function?..
var FF = currentFrame % fps;
var seconds = (currentFrame - FF) / fps;
var SS = seconds % 60;
var minutes = (seconds - SS) / 60;
var MM = minutes % 60;
var HH = (minutes - MM) / 60;
There you go.
It can be done in much simpler way:
function displayTime(currentFrame) {
var fps = 30;
var h = Math.floor(currentFrame/(60*60*fps));
var m = (Math.floor(currentFrame/(60*fps))) % 60;
var s = (Math.floor(currentFrame/fps)) % 60;
var f = currentFrame % fps;
return showTwoDigits(h) + ":" + showTwoDigits(m) + ":" + showTwoDigits(s) + ":" + showTwoDigits(f);
}
function showTwoDigits(number) {
return ("00" + number).slice(-2);
}
console.log("Frame 1020 will be displayed as " + displayTime(1020));
Frame 1020 will be displayed as 00:00:34:00
showTwoDigits
This help function takes a number (example: 6), adds "00" before it, which makes it a string (example: "006"). Then it slices back 2 positions from the end (will give "06").
displayTime
var h
It calculates hours by dividing the frames by 60*60*30 frames per hours. An hour has 60*60*30 frames.
var m
Minutes are calculated in the same way by dividing it by 60*30 frames per minute. But note here that this could result in a number like 80 minutes, because it is the TOTAL amount of minutes. The script has to take in account only the remainder after dividing this amount by 60. Here the modulus comes into play. 80 % 60 will give 20, the number we are looking for.
var s
In a similar way the seconds are calculated by dividing the frames by 30 frames per second and then take it modulus 60 (so that 65 seconds will be represented as 5).
Try this:
var fps = 30;
var currentFrame = 169;
var SS = Math.floor(currentFrame / fps);
var MM = Math.floor(SS / 60);
var HH = Math.floor(MM / 60);
var FF = currentFrame - (SS * fps);
function pad(str, width, what, left) {
str = String(str);
what = String(what);
var w = width - str.length;
if (left) {
return (new Array(w + 1)).join(what) + str;
} else {
return str + (new Array(w + 1)).join(what);
}
}
var i,
timecode = [HH, MM, SS, FF];
for (i = 0; i < timecode.length; i += 1) {
timecode[i] = pad(timecode[i], 2, 0, true);
}
var resultString = timecode.join(':'); // HH:MM:SS:FF
you can also use the date object see here. Just make something like:
var d = new Date( yourframetime + new Date().getTime() );
var str = d.getHours()+':'+ d.getMinutes()+ ':' + d.getSeconds() + .......
than you can use all the string functions of the object, or make your own with it.
Old post, but I was using this recently and a combination of Alexander's and Lucas's code give the correct results. The checked version actually breaks on really large frame counts ( I think due to Math.floor).
Code is:
var fps = 30;
var currentFrame = 169;
var FF = currentFrame % fps;
var seconds = (currentFrame - FF) / fps;
var SS = seconds % 60;
var minutes = (seconds - SS) / 60;
var MM = minutes % 60;
var HH = (minutes - MM) / 60;
function pad(str, width, what, left) {
str = String(str);
what = String(what);
var w = width - str.length;
if (left) {
return (new Array(w + 1)).join(what) + str;
} else {
return str + (new Array(w + 1)).join(what);
}
}
var i,
timecode = [HH, MM, SS, FF];
for (i = 0; i < timecode.length; i += 1) {
timecode[i] = pad(timecode[i], 2, 0, true);
}
var resultString = timecode.join(':'); // HH:MM:SS:FF
For Anyone interested with the swift version of the showTwoDigits function, here is a working code sample:
func showTwoDigits(number:Float) -> (String){
var string = ("00" + String(format:"%.f", number))
var range = Range(start: (advance(string.endIndex, -2)), end: string.endIndex)
var cutStr = string.substringWithRange(range)
return cutStr
}
This function converts to HH:MM:SS:FF correctly :
var convertTime = function (frames, fps) {
fps = (typeof fps !== 'undefined' ? fps : 30 );
var pad = function(input) {return (input < 10) ? "0" + input : input;},
seconds = (typeof frames !== 'undefined' ? frames / fps : 0 );
return [
pad(Math.floor(seconds / 3600)),
pad(Math.floor(seconds % 3600 / 60)),
pad(Math.floor(seconds % 60)),
pad(Math.floor(frames % fps))
].join(':');
}
Demo
var convertTime = function (frames, fps) {
fps = (typeof fps !== 'undefined' ? fps : 30 );
var pad = function(input) {return (input < 10) ? "0" + input : input;},
seconds = (typeof frames !== 'undefined' ? frames / fps : 0 );
return [
pad(Math.floor(seconds / 3600)),
pad(Math.floor(seconds % 3600 / 60)),
pad(Math.floor(seconds % 60)),
pad(Math.floor(frames % fps))
].join(':');
}
document.body.innerHTML = '<pre>' + JSON.stringify({
5 : convertTime(5),
50 : convertTime(50),
126 : convertTime(126),
1156 : convertTime(1156),
9178 : convertTime(9178),
13555 : convertTime(13555)
}, null, '\t') + '</pre>';
See also this Fiddle.

Averaging Times using Javascript

I am building an app using Phonegap and JQuery.
The app stores ( using window.localStorage ) a set of times (no more than 10) in the format.
HH:MM:SS.mm
There are a number of 'Zero' times in the list eg '00:00:00.00' which iphonegap, javascript
eliminate using..
function removeA(arr){
var what, a= arguments, L= a.length, ax;
while(L> 1 && arr.length){
what= a[--L];
while((ax= arr.indexOf(what))!= -1){
arr.splice(ax, 1);
}
}
return arr;
}
scores.sort();
removeA(scores,'00:00:00.00');
so that i'm left with the fastest time first, and only the times that have a value.
I need to produce from the remaining values the average of those times.
eg: 00:00:03.00
00:00:05.00
00:00:02.00
00:00:06.00
= 00:00:04.00
thanks in advance :)
var times= [ '00:00:03.00', '00:00:05.00', '00:00:02.00', '00:00:06.00'],
date = 0,
result = '';
function offsetify(t){
return t < 10 ? '0' + t : t;
}
for(var x = 0; x < times.length; x++ ) {
var tarr = times[x].split(':');
date += new Date(0, 0, 0, tarr[0], tarr[1], tarr[2].split('.')[0], tarr[2].split('.')[1]).getTime();
}
var avg = new Date(date/times.length);
result = offsetify(avg.getHours()) + ':' + offsetify(avg.getMinutes()) + ':' + offsetify(avg.getSeconds()) + '.' + offsetify(avg.getMilliseconds());
DEMO
if you are going to also have millisecond values and you want to consider them, then convert the times into millisecond. Now, add them and divide them by the number of records. Else, convert everything to seconds and find the average - you get the answer in seconds, of course.
The conversion is quite simple if take little time to think over it. Here's how to convert.
To milliseconds:
function convertToMS(timeStr) { // timeStr in format 'HH:MM:SS.mm'
var I = parseInt; // for brevity
var t = timeStr,
h = I( t.substr(0,2) ),
m = I( t.substr(3,2) ),
s = I( t.substr(6,2) ),
ms = I( t.substr(9,2) );
return h * 3600000 + m * 60000 + s * 1000 + ms;
}
To seconds:
function convertToS(timeStr) { // timeStr in format 'HH:MM:SS[.mm]' -- .mm is ignored.
var I = parseInt; // for brevity
var t = timeStr,
h = I( t.substr(0,2) ),
m = I( t.substr(3,2) ),
s = I( t.substr(6,2) );
return h * 3600 + m * 60 + s;
}
After the conversion's done, add them up and find the average.
UPDATE:
To convert back to the format 'HH:MM:SS.mm', we change back the time into 'chunks' of hours, minutes, seconds and (if applicable) milliseconds.
function chunkifyFromSec(time) { // time in s
var t = "",
h = Math.floor(time / 3600),
m = Math.floor( (t - (h * 3600)) / 60 ),
s = t - (h * 3600) - (m * 60);
return {
HH: h, MM: m, SS: s, mm: 0
};
}
function chunkifyFromMS(time) { // time in ms
var t = "",
h = Math.floor(time / 3600000),
m = Math.floor( (t - (h * 3600000)) / 60000 ),
s = Math.floor( (t - (h * 3600000) - (m * 60000)) / 1000 ),
mm = t - (h * 3600000) - (m * 600000) - (s * 1000);
return {
HH: h, MM: m, SS: s, mm: mm
};
}
Then, we return the string in the format 'HH:MM:SS.mm' using this:
function toTimeStr(chunks) {
return
(chunks.HH < 0 ? '0' : '') + chunks.HH + ":"
+= (chunks.MM < 0 ? '0' : '') + chunks.MM + ":"
+= (chunks.SS < 0 ? '0' : '') + chunks.SS + "."
+= (chunks.mm < 0 ? '0' : '') + chunks.mm
}
I don't have much experience with Javascript so I might have some syntax errors but I think you could do something like
var i = 0;
var totalTime = 0.0;
for (i=0; i < scores.length; i++) {
var hours = parseFloat(scores[i].substring(0, 2)); //get numeric value for hours
var minutes = parseFloat(scores[i].substring(3,5)); //get numeric value for minutes
var seconds = parseFloat(scores[i].substring(6)); //get numeric for the seconds
var time = ((hours * 60) + minutes) * 60 + seconds; //60 minutes in an hour, 60 seconds in a minute
totalTime += time;
}
var avgTime = totalTime/scores.length;
var avgHours = Math.floor(avgTime / 3600); //60*60
var avgHoursStr = String(avgHours);
var avgMinutes = Math.floor((avgTime % 3600) / 60); //mod to get rid of the hours
var avgMinutesStr = String(avgMinutes);
var avgSeconds = avgTime - avgHours*3600 - avgMinutes*60; //get the remainder. Can't use mod due to decimal
var avgSeconds = String(avgSeconds);
//Concat strings. Add the ":" spacers. Where necessary, add leading 0
var avgStr = (avgHoursStr.length > 1 ? "" : "0") + avgHoursStr + ":" + (avgMinutesStr.length > 1 ? "" : "0") + avgMinuteStr + ":" + avgSecondsStr;
[EDIT - Thanks to Parth Thakkar for point out my problem]
To return the answer in milliseconds or seconds:
var times = ["00:00:03.00", "00:00:05.00", "00:00:02.00", "00:00:06.00"];
function averageTimes(times,unit) {
if (!times) {
return false;
}
else {
var totalMilliseconds = 0, hours, minutes, seconds, milliseconds, parts;
for (var i = 0, len = times.length; i < len; i++) {
parts = times[i].split(':');
hours = parseInt(parts[0], 10) * 3600000;
minutes = parseInt(parts[1], 10) * 60000;
seconds = parseInt(parts[2].split('.')[0], 10) * 1000;
milliseconds = parseInt(parts[2].split('.')[1], 10);
totalMilliseconds += (hours + minutes + seconds + milliseconds);
}
if (!unit || unit.toLowerCase() == 'ms'){
return totalMilliseconds/times.length + ' milliseconds';
}
else if (unit.toLowerCase() == 's') {
return (totalMilliseconds/1000)/times.length + ' seconds';
}
}
}
// parameters:
// times: an array of times in your supplied format, 'HH:MM:SS:mm',
// unit: a string ('ms' or 's'), denoting whether to return milliseconds or seconds.
var average = averageTimes(times,'s');
console.log(average);​
JS Fiddle demo.
References:
for(){/*...*/} loop.
parseInt().
split().
toLowerCase().

Categories