JS日期操作

1、js设置倒计时

$(function(){

//一秒钟刷新一次 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); }

2、js比较日期大小

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); 
} 




你可能感兴趣的:(JS日期操作)