【问题标题】:Add 0 before minutes 1-9在 1-9 分钟前添加 0
【发布时间】:2016-09-06 10:22:23
【问题描述】:

我通过 jQuery 获取当前时间,然后根据时间做一些巧妙的操作来显示/隐藏开店标志。

我当前的代码运行良好,除了时间是 0-9 分钟后,在这种情况下它会失败并错误地显示 CLOSED 语句。原因是时间被渲染为 119,例如 11:09 - 我需要它写为 1109 而不是 119。

代码是:

 tday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

 function GetClock(){
 var d=new Date();
 var jwdday=d.getDay(),ap;
 var jwdtime = d.getHours() + "" + d.getMinutes(),gp;

 /* sign logic */
 if (jwdday != "0") {
   if((jwdtime < 1800 && jwdtime >= 1400) || (jwdtime < 1300 && jwdtime >= 700)) {
 document.getElementById('signStatus').innerHTML="<img src='http://www.hawkesbury-stores.co.uk/wp-content/themes/HawkSOS/images/core/weareOpen.png' alt='We are currently OPEN' title='We are currently OPEN' class='weareOpen'>";
   }
   else {
     document.getElementById('signStatus').innerHTML="<img src='weareClosed.png' alt='We are currently CLOSED' title='We are currently CLOSED' class='weareClosed'>";
   }
 }
 else if(jwdtime < 1230 && jwdtime >= 700){
 document.getElementById('signStatus').innerHTML="<img src='weareOpen.png' alt='We are currently OPEN' title='We are currently OPEN' class='weareOpen'>";
 }
 else {
 document.getElementById('signStatus').innerHTML="<img src='weareClosed.png' alt='We are currently CLOSED' title='We are currently CLOSED' class='weareClosed'>";
 }


 }

 window.onload=function(){
 GetClock();
 setInterval(GetClock,1000);
 }

【问题讨论】:

  • var min = minutes &gt; 10 ? minutes : "0" + minutes ; 这样的东西
  • 谢谢 - 是的,这种方法奏效了。

标签: jquery date time


【解决方案1】:

您似乎在算术上使用小时和分钟(将它们与数字进行比较),因此您可以执行类似的操作

var jwdtime = 100*d.getHours() + d.getMinutes();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2013-07-15
    • 2021-09-10
    • 1970-01-01
    • 2021-11-05
    • 2019-06-10
    相关资源
    最近更新 更多