javaScript时间动态显示

借助javaScript实现时间动态显示

 <script language="JavaScript">  
            function startTime()     
            {     
                var today=new Date();//定义日期对象     
                var yyyy = today.getFullYear();//通过日期对象的getFullYear()方法返回年      
                var MM = today.getMonth()+1;//通过日期对象的getMonth()方法返回年月     
                var dd = today.getDate();//通过日期对象的getDate()方法返回日期       
                var hh=today.getHours();//通过日期对象的getHours方法返回小时     
                var mm=today.getMinutes();//通过日期对象的getMinutes方法返回分钟     
                var ss=today.getSeconds();//通过日期对象的getSeconds方法返回秒     
                // 如果分钟或小时的值小于10,则在其值前加0,比如如果时间是下午3点20分9秒的话,则显示15:20:09     
                MM=checkTime(MM);  
                dd=checkTime(dd);  
                mm=checkTime(mm);     
                ss=checkTime(ss);      
                var day; //用于保存星期(getDay()方法得到星期编号)  
                if(today.getDay()==0)   day   =   "星期日 " ;  
                if(today.getDay()==1)   day   =   "星期一 " ;  
                if(today.getDay()==2)   day   =   "星期二 " ; 
                if(today.getDay()==3)   day   =   "星期三 " ;  
                if(today.getDay()==4)   day   =   "星期四 " ;  
                if(today.getDay()==5)   day   =   "星期五 " ; 
                if(today.getDay()==6)   day   =   "星期六 " ; 
                document.getElementById('nowDateTimeSpan').innerHTML=yyyy+"-"+MM +"-"+ dd +" " + hh+":"+mm+":"+ss+"   " + day;     
                setTimeout('startTime()',1000);//每一秒中重新加载startTime()方法   
            }     

            function checkTime(i)     
            {     
                if (i<10){  
                    i="0" + i;  
                }     
                  return i;  
            }   

            function setTimer(){  
                var targetDate = document.getElementById("target").value;  
                var taget = new Date(targetDate);  
                var now = new Date();  
                var plus = taget.getTime() - now.getTime();  
                var day = parseInt(plus/1000/60/60/24);  
                var hour = parseInt(plus/1000/60/60) - day * 24;  
                var minute = parseInt(plus/1000/60) - parseInt(plus/1000/60/60)*60;  
                var second = parseInt(plus/1000) - parseInt(plus/1000/60)*60;  
                document.getElementById("distanceTimeP").innerHTML = "距离"+targetDate+"还剩" + day + "天" + hour + "时" + minute + "分" + second + "秒";  
            }   
          setInterval(setTimer,1000);
        script>  
    head>  
    <body onload="startTime()"> 
        <center> 
         当前时间:<font color="red"><span id="nowDateTimeSpan">span>font><br>  
        目标时间:<input type="text" id="target" value="2020/01/01"/>
         <font color="green"><p id="distanceTimeP">p>font>    
        center>
    body>  
html>  

你可能感兴趣的:(javaweb,前端)