javascript - Working on a clock script. I need to add a Zero in front of single digits -
as easy may sound seasoned coder. newbie trying implement on clock page. can contain errors. idea generate 0 in front of single digits (like "02" instead of "2") display purposes. works fine double digits. got, doesn't trick. includes commented lines of different tries have done. appreciate input guys.
<script> $(function() { getdata(); myinterval = setinterval(getdata, 30000); }); function getdata(){ var dt = new date(); console.log(dt.getminutes()); var myhr = dt.gethours(); var mymin = dt.getminutes(); //if(myhr < 10) myhrstr = '0' + myhr.tostring(); else myhrstr = myhr.tostring(); //if(myhr.tostring().length < 2) myhrstr = '0' + myhr.tostring(); else myhrstr = myhr.tostring(); //if(myhr.tostring().length < 2) myhr = "0"+myhr; if(myhr.tostring().length == 1) myhrstr = "0" + myhr.tostring(); else myhrstr = myhr.tostring(); //if(mymin < 10) myminstr = '0' + mymin.tostring(); else myminstr = mymin.tostring(); //if(mymin.tostring().length < 2) myminstr = '0' + mymin.tostring(); else myminstr = mymin.tostring(); //if(mymin.tostring().length < 2) mymin = "0"+mymin; if(mymin.tostring().length == 1) myminstr = "0" + mymin.tostring(); else myminstr = mymin.tostring(); var mystr = myhrstr + myminstr; $.ajax( { url:"clock.php?action=getdata&dt="+mystr, success:function(result){ $('#content').html(result); } } ); } </script>
what makes think code isn't working? works fine.
here's demo:
function fakedate() { this.gethours = function() { return math.round(math.random() * 23); } this.getminutes = function() { return math.round(math.random() * 59); } } var dt = new fakedate(); // use fakedate random time generations var myhr = dt.gethours(); var mymin = dt.getminutes(); if (myhr.tostring().length == 1) myhrstr = "0" + myhr.tostring(); else myhrstr = myhr.tostring(); if (mymin.tostring().length == 1) myminstr = "0" + mymin.tostring(); else myminstr = mymin.tostring(); var mystr = myhrstr + myminstr; console.log(mystr);
one way can simplify code is, since not using numeric values, can call tostring
right away on gethours
, getminutes
methods. same reason there's no need variables hold string values, can use same variable when appending "0"
.
// strings representing hours , minutes var myhr = dt.gethours().tostring(); var mymin = dt.getminutes().tostring(); // prepend them zeros if needed if (myhr.length == 1) myhr = "0" + myhr; if (mymin.length == 1) mymin = "0" + mymin; // concatenate them 4 digit value var mystr = myhr + mymin;
here's demo:
function fakedate() { this.gethours = function() { return math.round(math.random() * 23); } this.getminutes = function() { return math.round(math.random() * 59); } } var dt = new fakedate(); // use fakedate random time generations // strings representing hours , minutes var myhr = dt.gethours().tostring(); var mymin = dt.getminutes().tostring(); // prepend them zeros if needed if (myhr.length == 1) myhr = "0" + myhr; if (mymin.length == 1) mymin = "0" + mymin; // concatenate them 4 digit value var mystr = myhr + mymin; console.log(mystr);
Comments
Post a Comment