简体   繁体   中英

What's wrong with this JavaScript/jQuery code?

Why won't my code show " 0 a.004? It shows "a.004 0 " instead.

 $(document).ready(function() { /* I will eventually replace my example with this var d = new Date(); var h = d.getUTCHours(); var m = d.getUTCMinutes(); var s = d.getUTCSeconds(); var d = new Date(); */ var h = 0; var m = 144; var s = 2; var u = (s / 864) + (m / 14.4) + (h / 0.24); // Tried as an example: var u = 11.060 u = u.toString(12); if (u + 1 < 12) { u = "0" + u; } var n = u.substring(0, 6); $("#time").append(n); }); 
 #time { color: #000; font-family: "Courier New"; font-size: 60pt; text-align: center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="time"></div> 

By the way, I got the metric time idea from this website , and decided to put it in base-12.

Looks like you need to compare against the 12 before your toString converting to base 12. Updated snippet in the below code:

 $(document).ready(function() { /* I will eventually replace my example with this var d = new Date(); var h = d.getUTCHours(); var m = d.getUTCMinutes(); var s = d.getUTCSeconds(); var d = new Date(); */ var h = 0; var m = 144; var s = 2; var u = (s / 864) + (m / 14.4) + (h / 0.24); var n = (u + 1 < 12 ? '0' : '') + u.toString(12).substring(0, 6); $("#time").append(n); }); 
 #time { color: #000; font-family: "Courier New"; font-size: 60pt; text-align: center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="time"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM