javascript 时间比较

 function SetDateDiff(vDateTime,msDateTime,cdDateTime,ms_e_DateTime,cd_e_DateTime)
        {
            var dS1=vDateTime; //服务器时间
            var dS=dS1;
            var dateObj1=new countDownObj(); 
            var i_Day=0,i_Hours=0,i_Minutes=0,i_Seconds=0,ms_Ok=false,cd_e=false,ms_e=false,cd_s=true;
           
            var dms_s=msDateTime;                 //秒杀开始时间
            var dCD_s=cdDateTime;                //秒杀开始倒计时时间
           
            dS=dS.replace(/-/g,'/');
            var vDate=new Date(dS);
            //开始时间
            dms_s=dms_s.replace(/-/g,'/');
            var s_date=new Date(dms_s);
           
            dCD_s=dCD_s.replace(/-/g,'/');
           
            var dms_e=ms_e_DateTime;            //秒杀结束时间
            var dCD_e=cd_e_DateTime;            //倒计时结束时间
            if(!compareDate(dS,dms_e)&&dS1!='')
            {//秒杀结束
                ms_e=true;
            }
            else if(!compareDate(dS,dCD_e))
            {//倒计时结束
                cd_e=true;
            }
            else if(!compareDate(dCD_s,dS))
            {//倒计时还未开始
                cd_s=false;
            }
            else if(compareDate(dS,dms_s))
            {//当前时间+1比开始时间小
                var ms=DateDiff(dS,dms_s);
                ms=ms/1000;
                if(ms>=86400)
                {
                    i_Day=Math.floor(ms/86400);
                    ms=ms%86400;
                }
                if(ms>=3600)
                {
                    i_Hours=Math.floor(ms/3600);
                    ms=ms%3600;
                }
                if(ms>=60)
                {
                    i_Minutes=Math.floor(ms/60);
                    ms=ms%60;
                }
                i_Seconds=ms;
                //当离秒杀时间还剩10s重新取服务器时间
                if(i_Day==0&&i_Hours==0&&i_Minutes==0&&i_Seconds==30){RefreshServerDateTime();}
            }
            if(i_Day==0&&i_Hours==0&&i_Minutes==0&&i_Seconds==0){ms_Ok=true;}
            if(cd_e==false||ms_e==true){ms_Ok=false;}//倒计时结束并秒杀还没结束
            dateObj1.i_Day=i_Day;
            dateObj1.i_Hours=i_Hours<10?'0'+i_Hours:i_Hours;
            dateObj1.i_Minutes=i_Minutes<10?'0'+i_Minutes:i_Minutes;
            dateObj1.i_Seconds=i_Seconds<10?'0'+i_Seconds:i_Seconds;
            dateObj1.ms_Ok=ms_Ok;
            dateObj1.ms_e=ms_e;
            dateObj1.cd_e=cd_e;
            dateObj1.cd_s=cd_s;
            return dateObj1;
        }
         function compareDate(startDate,endDate)
        {//比较日期 
            if(startDate=="" && endDate=="")
                return true;
            var strdt1=startDate.replace(/-/g, "/");
            var strdt2=endDate.replace(/-/g, "/");
            var dt1=new Date(Date.parse(strdt1));
            var dt2=new Date(Date.parse(strdt2));
 
            if(dt1<=dt2)
                return true;
            else
                return false;
        }

你可能感兴趣的:(JavaScript)