Format Date in javascript for "from here to Xd Yh zm"

0

Friends, I am trying to format a date in javascript to stay in the following format:

In x days, and Hours, Z minutes.

But my code is not working properly:

dt = dados[i].Hora - dataAtual; // o valor está em segundos

todos_min = Math.round(dt/60);

min_restantes = Math.round(todos_min%60);

todas_hrs = Math.round(todos_min/60);

hrs_restantes = Math.round(todas_hrs%24);

todos_dias = Math.round(todas_hrs/24);
console.log( todos_dias+'d '+hrs_restantes+'h '+min_restantes+'m' );

Could someone give me a light?

Thanks in advance.

    
asked by anonymous 30.07.2014 / 15:15

2 answers

1

Hello, I'll try to be as educational as possible so that more people can take advantage of this response in the future.

Although you CAN start by selecting the minutes, then the hours and then the days, it is usually easier to do the reverse. The diagram shows the general idea of how to transform from a smaller unit (in your case seconds ) to larger units (in your case, minutes , in> and days ).

Theideaisthat,withtheinputmeasuredinthesmallestunit(seconds),thealgorithmfindsina1ststephowmanytimesthelargestunitinput-onthediagram,are3times.Theninstep2,theprogramwillfindhowmanytimesthesecondlargestunit(hours)fitswithintherestofthe1ststep,andsoonuntilthedesiredaccuracyisreached.

Animplementationofthisalgorithmis in this jsFiddle , with several comments to aid understanding.

Two dangerous details I noticed in your code were:

  • Math.round () : rounds to the nearest integer, either greater or smaller . For example: 1,5 and 1,598 and would be rounded to 2; since 1.499 and 1.0 would be rounded to 1. Therefore, I found the use of this function dangerous, because to eliminate surpluses in each step, we do not want the algorithm to go up in any case. I suggest using Math.floor () , which always rounds down.

  • Although you find out how many minutes remain, you use the total minutes to get the times , and the same thing repeats for the hours in relation to the days . Suppose, for example, that the total number of hours is 15 (% with%). According to its algorithm, the remaining hours (% with%) would be 15 (which is correct), but the number of days (% with%) would be equal to todas_hrs=15 which results in ... 1 !!!

By adjusting these details, you will realize that it is not impossible to do your method, but you need more attention, which is why I recommend using the most popular algorithm, which I indicated, okay?

I hope I have helped! Hugs!

    
30.07.2014 / 19:54
0

Make an .html file with this code

Your code is working normally.

<html>
<body>
<script>    

   dt = 86400; //1d 0h 0s o valor está em segundos

   todos_min = Math.round(dt/60);

   min_restantes = Math.round(todos_min%60);

   todas_hrs = Math.round(todos_min/60);

   hrs_restantes = Math.round(todas_hrs%24);

   todos_dias = Math.round(todas_hrs/24);

   document.write( todos_dias+'d '+hrs_restantes+'h '+min_restantes+'m' );
</script>
</body>
</html>
    
30.07.2014 / 15:51