Skip to content

Instantly share code, notes, and snippets.

@infomaven
Last active May 7, 2021 12:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infomaven/6096276 to your computer and use it in GitHub Desktop.
Save infomaven/6096276 to your computer and use it in GitHub Desktop.
JavaScript function that converts Hrs & Minutes into Decimal Hrs. It uses a Hash structure in Javascript to store the equivalencies. Intended audience: Use by Employees who need to enter Decimal time values for their time cards at work. Further down the line, this logic will be refactored into a Service that can be used programmatically by time-…
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta name="description" content="[add your bin description]" />
<meta charset=utf-8 />
<title>Decimalizer c.2013 - Nadine Whitfield</title>
</head>
<body>
<form action="demo_form.asp" autocomplete="on">
Hours:<input type="number" name="hrs" min=0 max=12 step=1><br>
Minutes: <input type="number" min=0 max=60 step=1 name="mins"><br>
<br>
<input type="submit">
</form>
</p>
<div>********************************</div>
<div>
<p>Hours: </p></br>
<p>Minutes entered: </p></br>
<p> conversion: </p>
</div>
</body>
</html>
var hundrs = {
1: 0.02,
2: 0.03,
3: 0.05,
4: 0.07,
5: 0.08,
6: 0.10,
7: 0.12,
8: 0.13,
9: 0.15,
10: 0.17,
11: 0.18,
12: 0.20,
13: 0.22,
14: 0.23,
15: 0.25,
16: 0.27,
17: 0.28,
18: 0.30,
19: 0.32,
20: 0.33,
21: 0.35,
22: 0.37,
23: 0.38,
24: 0.40,
25: 0.42,
26: 0.43,
27: 0.45,
28: 0.47,
29: 0.48,
30: 0.50,
31: 0.52,
32: 0.53,
33: 0.55,
34: 0.57,
35: 0.58,
36: 0.60,
37: 0.62,
38: 0.63,
39: 0.65,
40: 0.67,
41: 0.68,
42: 0.70,
43: 0.72,
44: 0.73,
45: 0.75,
46: 0.77,
47: 0.78,
48: 0.80,
49: 0.82,
50: 0.83,
51: 0.85,
52: 0.87,
53: 0.88,
54: 0.90,
55: 0.92,
56: 0.93,
57: 0.95,
58: 0.97,
59: 0.98,
60: 0.00
};
console.log(' Now getting values. ');
for (var i in hundrs ) {
console.log( hundrs[i] );
}
console.log(' Now getting value for index 3');
console.log( hundrs[3] );
console.log(' Add an hour input to this map value:');
console.log( 1 + hundrs[3] );
conosole.log('TODO: Write values to the webpage instead of the console...');
document.echo( 1 + hundrs[3] );
alert( hundrs[3] );
@infomaven
Copy link
Author

This is the groundwork for a web page that can be used to convert timecard entries from Hours & Minutes into Decimal equivalents. It seems most time keeping systems use this format..

@infomaven
Copy link
Author

Decimal equivalencies will be stored client-side in a JS Hashmap. Once the HTML form is wired up, the javascript will grab those values out of the form, convert the minutes portion and display the converted minutes + inputted hours as a final result.

@rafal-1990
Copy link

You don't need this ugly hashmap. You can use a function like this to convert "hh:mm" into decimal:

function timeToDecimal(t) {
    t = t.split(':');
    return parseFloat(parseInt(t[0], 10) + parseInt(t[1], 10)/60).toFixed(2);
}  

//example
for(var i=1;i<=60;i++) {
myTime="00:"+((i.toString().length == 1)?"0":"")+i.toString() ;
console.log( myTime+" = "+timeToDecimal(myTime) );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment