jQuery交付倒计时

时间:2015-08-10 15:41:57

标签: javascript jquery timer

该商店的最新发货时间是星期一至星期五下午5点。我已经想过如何使用标准的JS数据对象创建每天都显示相同内容的东西,这是非常基本但不能完成我需要它做的事情。

有什么想法吗?我应该走下面代码的方式,或尝试使用像moment.js这样的方式吗?

setInterval(function () {

    var now = new Date();
    var year = now.getYear();
    var month = now.getMonth();
    var day = now.getDay();
    var hour = now.getHours();
    var end;

    if(day >= 1 && day <= 5) {

        // Not the weekend
        if (hour < 17) { 
            // Before 5 not on weekends
            // end = 5 today
            end = new Date(year, month, day, 17, 0, 0, 0);  
            console.log('before 5');
        } else if (hour >= 17 && day == 5) {
            // After 5 on friday. Set for monday
            // end = 5 monday
            //end = new Date(now.getYear(), now.getMonth(), day + 1, 17, 0, 0, 0); 
            console.log('after 5 on friday');
        } else {
            // After 5 monday-thursday
            // end = 5 tomorrow
            console.log('after 5 not on friday');
            end = new Date(year, month, day + 1, 17, 0, 0, 0); 
        }

    } else {
        // Weekend
        // end = next monday
        console.log('Shipped monday');
    }

    var timeleft = end.getTime() - now.getTime();
    var diff = new Date(timeleft);

    console.log("Order now and we ship your order in: " + diff.getHours() + "h " + diff.getMinutes() + "Min " + diff.getSeconds() + "Sec");    

}, 1000);

我需要它在下午5点之后显示倒数到第二天的交货时间。周五下午5点后显示周一交货时间,周六和周日也是如此。

2 个答案:

答案 0 :(得分:3)

这应该这样做:

&#13;
&#13;
params.require(:emission).permit(:name, :key, :phone, :address, :country, :state, :city, :email, :template, :content, {news_source: []})
&#13;
&#13;
&#13;

答案 1 :(得分:0)

像这样的东西

function pad(num) { return ("0"+num).slice(-2)}
function getHHMMSS(ms) {
  var seconds = parseInt(ms / 1000) % 60 ;
  var minutes = parseInt(ms / (1000*60)) % 60;
  var hours   = parseInt(ms / (1000*60*60)) % 24;
  console.log(parseInt(ms / 1000) % 60 )
  var str = pad(hours)+" hour"+(hours==1?"":"s")+" "+
    pad(minutes)+" minute"+(minutes==1?"":"s")+" "+
    pad(seconds)+" second"+(seconds==1?"":"s");
  return str;
}

setInterval(function () {
    var now = new Date(); // server time
    var end = new Date(now.getTime());
    var hour = now.getHours()+now.getMinutes()/60;
    if (hour>17) end.setDate(end.getDate()+1); // too late today
    var day = end.getDay();
    end.setHours(17,0,0,0); 
    if (!(day>=1 && day<=5)) { // weekend
      end.setDate(end.getDate()+(day==0?1:2)); // add one on sunday 2 on saturday
    }
    var diff = end.getTime() - now.getTime();
    document.getElementById("open").innerHTML="Order now and we ship your order in: " + 
      getHHMMSS(diff) +"<br/>("+new Date(now.getTime()+diff)+")";
},500);
<span id="open"></span>