Rounding time in JavaScript - javascript

I am trying to figure out a formula to calculate the fraction of an hour. I want to break up an hour into six-minute intervals. This means I would have the following table:
Input Output
----- ------
5 hrs 0 mins 5.0
5 hrs 1 min 5.0
5 hrs 2 mins 5.0
5 hrs 3 mins 5.0
5 hrs 4 mins 5.1
5 hrs 5 mins 5.1
5 hrs 6 mins 5.1
5 hrs 7 mins 5.1
5 hrs 8 mins 5.1
5 hrs 9 mins 5.1
5 hrs 10 mins 5.2
5 hrs 11 mins 5.2
5 hrs 12 mins 5.2
5 hrs 13 mins 5.2
5 hrs 14 mins 5.2
5 hrs 15 mins 5.2
5 hrs 16 mins 5.3
...
Currently, I have the following:
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var fraction = Math.floor(m / 60);
var result = h + '.' + fraction;
The fraction part is not working right. There is something about rounding, etc. that I'm not sure how to handle in this situation. I would be thankful for anyone that can help me with this problem.
Thanks!

You have to apply a little logic to properly format a duration with javascript. The logic is needed to consider the final minutes in an hour. The following should work for you:
function FormatDuration(duration) {
// Retrieve the hours and minutes
var hrs = duration.getHours();
var mins = duration.getMinutes();
// Convert the minutes to a fraction of an hour.
var tenths = ((mins / 60).toFixed(1) * 10);
if (tenths === 10) {
tenths = 0;
hrs = hrs + 1;
}
return hrs + '.' + tenths;
}

Dividing by 60 will always give you a value between 0 and 1, meaning floor() will always return 0 (unless m is equal to 60). You can divide by 6 instead:
var fraction = Math.floor(m / 6);
Tests:
Math.floor(6 / 6); // 1
Math.floor(3 / 6); // 0
Math.floor(30 / 6); // 5
Math.floor(59 / 6); // 9

I don't know if this is correct but this seems to match your numbers:
var fraction = Math.round(m/6.1) / 10;
I used 6.1 to push the numbers down one as with just 6 3 minutes becomes .1 instead of 4 minutes becoming .1
This leads to 58 and 59 minutes being a whole 1.0 which would bump up your hours to the next. Not sure if that is what you want as your samples only go up to 16 minutes.
I came up with this by playing in excel with the numbers from 0 to 59. It's a great way to test these types of issues.

Related

Javascript milliseconds 3 decimal places [duplicate]

This question already has answers here:
Is there a JavaScript function that can pad a string to get to a determined length?
(43 answers)
Closed 1 year ago.
Currently, my script is working as expected, but I want to show milliseconds in 3 decimal places less than 100
Here is my function:
var updateTime = function () {
var tempTime = elapsedTime;
var milliseconds = tempTime % 1000;
tempTime = Math.floor(tempTime / 1000);
var seconds = tempTime % 60;
tempTime = Math.floor(tempTime / 60);
var minutes = tempTime % 60;
tempTime = Math.floor(tempTime / 60);
var hours = tempTime % 60;
time = (hours) + ":" + (minutes) + ":" + (seconds) + "." + milliseconds;
console.log(milliseconds)
document.getElementById('time').innerText = time
};
console.log output:
0
0
2
4
5
10
14
17
21
25
30
33
38
41
46
...
81
85
89
93
97
102
105
109
113
117
122
125
How can I make the output be like this? =>
000
000
002
004
005
010
014
017
021
025
046
...
081
085
089
093
097
102
105
I tried using milliseconds.toFixed(3) but nothing happened! Could you please help me?
Thank you.
Try this
const fmtNum = (num, pad) => String(num).padStart(pad, "0");
time = `${fmtNum(hours, 2)}:${fmtNum(minutes, 2)}:${fmtNum(seconds, 2)}.${fmtNum(milliseconds, 3)}`;

formet in 24 hour inn clock function

Hello every one I am writing a abir John and want to find the clock Javascript file
That's code add in javascript file then link in index.html file
function clock(){
var hour = document.getElementById('hour');
var minute = document.getElementById('minute');
var seconds= document.getElementById('seconds');
var amp= document.getElementById('amp');
if(h>12){
h =h-12;
var am = "PM"
} ```
I think you are trying to convert 24 hrs format to 12 hrs format.
If you want to convert your hour to 12 hours format you can take % 12 on the current time.
If the time is 13 then 13 % 12 → 1
time = 23 then 23 % 12 → 11
time = 24, then 24 % 12 → 0, if the time is 0, then change the time as 12.
if(h>=12){
h = h%12 || 12;
}

javascript hours to real hours

I have a piece of code like this:
window.setInterval("reloadIFrame();", 3000);
^
*
I want to know if there is a chart anywhere that can translate js time (*) to real hours, like one hour 2 hour three hour is there any way?
That 3000 is just expressed in milliseconds, so standard math will do.
3000ms = 3000ms / 1000ms/s / 3600s/h = .00083 hours
The parameter does not accept hours, you would have to multiply to get it in millisecond.
1000 ms = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
2 hours -> 60 min * 2 hours = 120 minutes * 60 = 7,200 seconds * 1000 = 7,200,000 ms
The reverse would be a division.
3000 ms / 1000 = 3 seconds / 60 = 0.05 minute / 60 = 0.00083 hours

calculate differences between two time in minutes [duplicate]

This question already has answers here:
calculate time difference
(4 answers)
Closed 9 years ago.
How to calculate differences between two time eg(server time=04:30 pm and <p id="orderTime">02:30 pm</p>) and return it in minutes such as 90 minutes using jquery and javascript. The server time and order time is in 12 hour format not in 24h.
If you can guarantee they always will be of that format, then here is a straightforward solution:
function humanReadableToMinutes(time)
{
var parts = time.split(/ |:/);
return (parts[2] == 'pm' * 12 * 60)
+ parseInt(parts[0], 10) * 60
+ parseInt(parts[1], 10);
}
http://jsfiddle.net/aYwux/2/
Explanation:
By var parts = time.split(/ |:/); we're splitting 02:30 pm into 3 parts: 02, 30, pm.
return (parts[2] == 'pm' * 12 * 60) + parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10); contains of 3 operands:
* (parts[2] == 'pm' * 12 * 60) --- adds additional 12 hours if it's "pm"
* parseInt(parts[0], 10) * 60 - takes hours fraction extracted and converts it to minutes
* parseInt(parts[1], 10) - minutes fraction
PS: this solution doesn't take care of 12am and 12pm accurately, so it's a homework for the OP

JDE/Julian Time: How to format julian time stamp number

In my system, time stamps are returned using the old IBM julian format.
For example:
12 o'clock 0 minutes and 1 seconds AM (1 sec after midnight) is returned 01.
12 o'clock 22 minutes and 15 seconds AM is returned 2215.
1 o'clock 22 minutes and 15 seconds AM is returned 12215.
7 o'clock 45 minutes and 1 seconds AM is returned 74501.
7 o'clock 22 minutes and 15 seconds PM is returned 192215.
I need a regex expression to put these into the format of:
12 o'clock 0 minutes and 1 seconds AM (1 sec after midnight): 00:00.01
12 o'clock 22 minutes and 15 seconds AM: 00:22.15
1 o'clock 22 minutes and 15 seconds AM: 01:22.15
7 o'clock 45 minutes and 1 seconds AM: 7:45.01
7 o'clock 22 minutes and 15 seconds PM: 19:22.15
Any help is appreciated.
SOLUTION
Thanks to MikeM, here is the solution:
//var time = '01';
//var time = '2215';
//var time = '12215';
//var time = '74501';
var time = '192215';
time = time.replace( /^(?:(?:(\d)?(\d))?(\d\d))?(\d\d)$/,
function ( all, hr1, hr2, min, sec ) {
return (hr1 || '0') + (hr2 || '0') + ':' + (min || '00') + '.' + sec;
}
);
The following works with your examples, though I haven't tested it beyond that
//var time = '01';
//var time = '2215';
//var time = '12215';
//var time = '74501';
var time = '192215';
time = time.replace( /^(?:(?:(\d)?(\d))?(\d\d))?(\d\d)$/,
function ( all, hr1, hr2, min, sec ) {
return (hr1 || '0') + (hr2 || '0') + ':' + (min || '00') + '.' + sec;
}
);
Although it gives 07:45.01 not 7:45.01, so as to be in keeping with 01:22.15.
I'll give you a clue:
Convert returned value to a number.
num % 100 is the seconds.
(num / 100) % 100 is the minutes.
(num / 10000) is the hours.
If the hours is less than 12, use AM
If the hours is 12 or more, use PM and further, if its 13 or more, subtract 12.
Another way to do it is to treat it as a string. But then you have to add enough leading zeros to get to length 6 and then break it into 2 character bits and convert each to an 'int' and that's way more work than just mod-ing by 100 and diving by 100 and 10,000.
There should never be a value in those two digit sections greater than 59.
Note
#radi8 noticed something I left out. I should have noted that the "/" (division) in the above algorithm has to be integer arithmetic for it to work right. Some programming languages offer integer arithmetic. JavaScript does not.
Since JavaScript uses floating point arithmetic, he subtracts off the number of seconds before dividing. Then a similar subtraction of the number of minutes fixes the next step.
You could also use Math.floor() after dividing to accomplish the same thing (since these are positive numbers).
Here is OP's code modified to do that:
$(function () {
var val1 = 41215,hr=0,min=0,sec=0;
sec = val1%100;
val1 = Math.floor(val1 / 100);
min = val1%100;
hr = Math.floor(val1 / 100);
// format the result. This example could show 1:1:1 instead of 01:01:01
tst2 = hr.toString()+':'+min.toString()+'.'+sec.toString();
alert(tst2.toString());
});

Categories