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

Asked

Viewed 85 times

0

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

In x Days, y Hours, Z minutes.

Only my code isn’t 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' );

Someone could give me a light?

Thank you in advance.

  • Your code is working normally buddy, could better describe your error?

  • The code has no execution errors, but I noticed some logic problems. I know this kind of inattention happens, so I’ve given my answer a lot nay for thinking you need, but mainly to help other people who might have the same problem during your programming studies phase :D I hope it helps! Hugs!

2 answers

1

Hello, I will try to be as didactic as possible, so that more people can take advantage of this answer in the future.

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

Rascunho: convertendo um valor (em uma unidade pequena) para unidades maiores.

The idea is that with the measured input in the smallest unit (seconds), the algorithm finds in a first step how many times the largest unit (days) fits inside that input -- in the diagram, it is 3 times. Then, in the 2nd step, the program will find how many times the second largest unit (hours) fits inside the remnant of the first step, and so on until the desired precision is achieved.

An implementation of this algorithm is found in this jsFiddle, with several comments to help the understanding.

Two dangerous details I noticed in your code were:

  • Math.round(): rounds to the nearest integer value, be it greater or minor. For example: 1.5 and 1.598 and would be rounded to 2; 1.499 and 1.0 would be rounded to 1. Therefore, I found dangerous the use of this function, because, to eliminate the surpluses at each stage, we do not want the algorithm to round up in any case. I suggest using the Math.floor(), that always round down.

  • Although you figure out how many minutes are left, you uses the total minutes to get the hours, and the same thing if repeats for hours in relation to days. Suppose, for example, that the total hours is 15 (todas_hrs=15). According to your algorithm, the remaining hours (hrs_restantes = Math.round(todas_hrs%24)) would be 15 (which is correct), but the number of days (todos_dias = Math.round(todas_hrs/24)) would equal Math.round(15/24) which results in... 1!!!

Adjusting these details, you’ll realize it’s not impossible make your method, but need more attention, reason why I recommend using the most popular algorithm, which I indicated, ok?

I hope I helped! Hugs!

0

Make a . 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.