Last active
May 14, 2023 14:41
-
-
Save keejelo/506713bf68eb99a02d425bb4c597de38 to your computer and use it in GitHub Desktop.
Convert total time into seconds. Result is displayed in decimal, hex little/big endian. Also can convert back to time: hh:mm:ss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <meta charset="utf-8"> | |
| <title>Time converter</title> | |
| <style> | |
| body | |
| { | |
| margin:0; | |
| padding:20px; | |
| font-family:monospace; | |
| font-size:16px; | |
| } | |
| input | |
| { | |
| font-family:monospace; | |
| font-size:16px; | |
| } | |
| table | |
| { | |
| height:150px; | |
| border-collapse:collapse; | |
| } | |
| td | |
| { | |
| border:1px #ccc solid; | |
| padding:10px; | |
| } | |
| </style> | |
| <script> | |
| window.addEventListener('load', function() | |
| { | |
| let form1 = document.getElementById('form1'); | |
| if(form1) | |
| { | |
| form1.addEventListener('submit', function(e) | |
| { | |
| let hours = document.getElementById('hours'); | |
| let mins = document.getElementById('mins'); | |
| let secs = document.getElementById('secs'); | |
| hours.value = hours.value.replace(/[^0-9]/g, '').trim(); | |
| mins.value = mins.value.replace(/[^0-9]/g, '').trim(); | |
| secs.value = secs.value.replace(/[^0-9]/g, '').trim(); | |
| let total = parseInt(hours.value * 3600) + parseInt(mins.value * 60) + parseInt(secs.value); | |
| let totalHex = total.toString(16); | |
| // ** Zeropad front of hexstring if not even | |
| if(totalHex.length % 2) | |
| { | |
| totalHex = '0' + totalHex; | |
| } | |
| // ** Convert into little endian | |
| let totalHexLittleEndian = totalHex.match(/.{1,2}/g); | |
| totalHexLittleEndian = totalHexLittleEndian.reverse(); | |
| totalHexLittleEndian = totalHexLittleEndian.join(); | |
| totalHexLittleEndian = totalHexLittleEndian.replace(/,/g,''); | |
| // ** Display results | |
| document.getElementById('dec').innerHTML = total; | |
| document.getElementById('hexBig').innerHTML = '0x' + totalHex.toUpperCase(); | |
| document.getElementById('hexLittle').innerHTML = '0x' + totalHexLittleEndian.toUpperCase(); | |
| e.preventDefault(); | |
| return false; | |
| }); | |
| } | |
| let form2 = document.getElementById('form2'); | |
| if(form2) | |
| { | |
| form2.addEventListener('submit', function(e) | |
| { | |
| let tot_time = document.getElementById('tot_time'); | |
| tot_time = tot_time.value.replace(/[^0-9A-FXa-fx]/g, '').trim(); | |
| document.getElementById('tot_time').value = tot_time; | |
| // ** If hex string | |
| if(tot_time.indexOf('0x') != -1) | |
| { | |
| parseInt(tot_time, 16); | |
| } | |
| else | |
| { | |
| parseInt(tot_time, 10); | |
| } | |
| // ** Calculate hours | |
| let hours = Math.floor(tot_time / 3600); | |
| if(hours < 10) | |
| { | |
| hours = '0' + hours; | |
| } | |
| // ** Calculate minutes | |
| let mins = Math.floor((tot_time / 60) % 60); | |
| if(mins < 10) | |
| { | |
| mins = '0' + mins; | |
| } | |
| // ** Calculate seconds | |
| let secs = tot_time % 60; | |
| if(secs < 10) | |
| { | |
| secs = '0' + secs; | |
| } | |
| // ** Display results | |
| document.getElementById('result2').innerHTML = hours + ':' + mins + ':' + secs; | |
| e.preventDefault(); | |
| return false; | |
| }); | |
| } | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <div> | |
| <p>Get total time in seconds</p> | |
| <form id="form1" action="#" method="get"> | |
| <p>Hours <input id="hours" type="text" value="0" placeholder="hours" /></p> | |
| <p>Mins <input id="mins" type="text" value="0" placeholder="minutes" /></p> | |
| <p>Secs <input id="secs" type="text" value="0" placeholder="seconds" /></p> | |
| <p><input type="submit" id="btn" value="Calculate seconds" /></p> | |
| </form> | |
| <div> </div> | |
| <div>Result in seconds:</div> | |
| <div> </div> | |
| <div> | |
| <table id="result"> | |
| <tr> | |
| <td>Decimal</td> | |
| <td id="dec">0</td> | |
| </tr> | |
| <tr> | |
| <td>Hex (big endian)</td> | |
| <td id="hexBig">0x00</td> | |
| </tr> | |
| <tr> | |
| <td>Hex (little endian)</td> | |
| <td id="hexLittle">0x00</td> | |
| </tr> | |
| </table> | |
| </div> | |
| </div> | |
| <div> </div> | |
| <hr> | |
| <div> </div> | |
| <div> | |
| <p>Get time from seconds</p> | |
| <p>(if hexadecimal then start with: 0x )</p> | |
| <form id="form2" action="#" method="get"> | |
| <p>Secs <input id="tot_time" type="text" value="0" placeholder="seconds" /></p> | |
| <p><input id="btn2" type="submit" value="Calc time" /></p> | |
| </form> | |
| <div> </div> | |
| <div>Result (hh:mm:ss):</div> | |
| <div> </div> | |
| <div id="result2">00:00:00</div> | |
| </div> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment