$(function(){ firefox
//一秒鐘刷新一次 setInterval(getTime, 1000); getTime(); }); function getTime(){ //在firefox和ie下,若是時間裏是-分割,獲得的時間是NaN,換成/就行了 var oDateEnd=new Date('{$group_buy.end_time}'.replace("-", "/").replace("-", "/")); var oDateNow=new Date(); var iDay=0; var iHour=0; var iMin=0; var iSec = 0; iRemain=(oDateEnd.getTime()-oDateNow.getTime())/1000; if(iRemain > 0){ iDay=parseInt(iRemain/86400); iRemain%=86400; iHour=parseInt(iRemain/3600); iRemain%=3600; iMin=parseInt(iRemain/60); iRemain%=60; iSec=parseInt(iRemain); } $('#dountdownDay').text(iDay); $('#dountdownHour').text(iHour); $('#dountdownMin').text(iMin); $('#dountdownSec').text(iSec); }
二、js比較日期大小
code
function checkTime(){ var now = new Date(CurentTime()); if(now>endTime){ alert('活動已經結束了,不能報名了,下次趕早吧'); return false; } if(now>startTime){ alert('活動已經開始不能報名了,下次趕早吧'); return false; } return true; } function CurentTime() { var now = new Date(); var year = now.getFullYear(); //年 var month = now.getMonth() + 1; //月 var day = now.getDate(); //日 var hh = now.getHours(); //時 var mm = now.getMinutes(); //分 var clock = year + "/"; if(month < 10) clock += "0"; clock += month + "/"; if(day < 10) clock += "0"; clock += day + " "; if(hh < 10) clock += "0"; clock += hh + ":"; if (mm < 10) clock += '0'; clock += mm+":00"; return(clock); }