C#代码:
//算出用户填写倒计时 var dtr = remodle.UpdateTime.AddDays(3); var dt = DateTime.Now; var t = dtr.Subtract(dt).Duration();ViewBag.second = t.TotalSeconds;
//注意:这里有一个小bug -TotalSeconds这个方法返回的是绝对值
js代码:
//倒计时 $(document).ready(function () { var intDiff = parseInt($(".countDown").html()); countDown(intDiff); }); var time; function countDown(intDiff) { time = window.setInterval(function () { var day = 0, hour = 0, minute = 0, second = 0;//时间默认值 if (intDiff > 0) { day = Math.floor(intDiff / (60 * 60 * 24)); hour = Math.floor(intDiff / (60 * 60)) - (day * 24); minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60); second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60); } else { stopcountDown(time); } $(".countDown").html(day + '天' + hour + "小时" + minute + "分钟" + second + "秒"); intDiff--; }, 1000); } function stopcountDown() { clearInterval(time), function() { time = setInterval("countDown", 1000); };
}
html代码:
<p class="gray-text">剩余时间:<span class="countDown">@ViewBag.second</span></p>