JavaScript实现动态时间效果

首先说明JavaScript日期相关的一些操作。

1.创建日期对象  

   var dateObj = new Date()//返回当前系统时间,Thu Jan 20 2011 23:25:24 GMT+0800

2.获取时针数字

   dateObj.getHours() // 23

 

3.获取分针数字

   dateObj.getMinutes() //25

 

4.获取秒针数字

   dateObj.getSeconds() //24

 

5.获取星期几

   dateObj.getDay() // 4

 

6.获取日期

   dateObj.getDate() // 20

 

7.获取月份

   dateObj.getMonth() // 0, 月份的索引由0开始,因此,如果返回0,则需加1才得出真正的月份

 

8.获得年份

  (1) dateObj.getYear()

  关于这个方法要特殊说明一下,这个方法的返回值在firefox和IE中不同:在标准浏览器中都返回 111,因为标准的计算起始年份是从1900年,因此需要加上1900才得出真正的年份;而在IE中,直接返回2011,无需特别操作。

  所以此处要做浏览器类型判断,具体判断方法参见这里

  (2) dateObj.getFullYear()/dateObj.getUTCFullYear()

  使用这个方法就不用想上述方法那样麻烦,可以直接返回正确的年份。

 

 

动态时间效果一:YYYY-MM-DD hh:mm:ss

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 

<title>电子时钟</title> 

</head> 

<style type="text/css"> 

#showtime{ background:#333; color:#FFF; height:30px; line-height:30px; font-size:12px; textindent:30px; width:250px; border:2px #999 solid; } 

#localtime{ margin-lift:10px; color:#CCC; } 

</style> 

<body> 

<div id="showtime"> 

<span id="localtime"></span> 

</div> 

<script language="javascript"> 

function showLocale(objD) 

var str; 

var hh = objD.getHours(); 

var mm = objD.getMinutes(); 

var ss = objD.getSeconds(); 

var month = objD.getMonth()+1;

var date = objD.getDate();

 

if(hh<10)hh='0'+hh; 

if(mm<10)mm='0'+mm; 

if(ss<10)ss='0'+ss; 

if(month<10)month='0'+month;

if(date<10)date = '0' + date;

 

var year = objD.getFullYear();

 

str=year+"年";

str+=month+"-"+date+" "; 

 

 

str+= hh+":"+mm+":"+ss; 

return(str); 

 

function tick() 

var today; 

today=new Date(); 

document.getElementById("localtime").innerHTML=showLocale(today); 

 

window.setInterval("tick()",1000); 

 

</script> 

</body> 

</html>

 

 

动态时间效果二:倒计时,DD天hh小时mm分钟ss秒

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 

<title>电子时钟</title> 

</head> 

<style type="text/css"> 

#showtime{ background:#333; color:#FFF; height:30px; line-height:30px; font-size:12px; textindent:30px; width:250px; border:2px #999 solid; } 

#localtime{ margin-lift:10px; color:#CCC; } 

</style> 

<body> 

<div id="showtime"> 

<span id="localtime"></span> 

</div> 

<script language="javascript"> 

function countDown(ls) 

   var str;    

 

   var date = parseInt(ls/(24*60*60*1000)) ; ls = ls % 86400000;

   var hh = parseInt(ls/3600000) ; ls = ls % 3600000;

   var mm = parseInt(ls/60000) ; 

   var ss = parseInt(Math.round(ls%60000)/1000);

 

   if(hh<10)hh='0'+hh; 

   if(mm<10)mm='0'+mm; 

   if(ss<10)ss='0'+ss; 

   if(date<10)date = '0' + date;

 

   str="还有:"+date+"天"+hh+"小时"+mm+"分钟"+ss+"秒 结束"; 

   return(str); 

 

function tick() 

var curTime = (new Date()).getTime(); //采用毫秒形式

var leftTime = endTime-curTime;

 if(leftTime>0){

   document.getElementById("localtime").innerHTML=countDown(leftTime); 

 }else{

   document.getElementById("localtime").innerHTML="已结束"; 

   clearInterval(tm);

 

 }

 

var endTime= 1315140461784; //结束时间

var tm=setInterval(tick,1000);

 

</script> 

</body> 

</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是JavaScript实现动态时钟的代码: HTML: ```html <!DOCTYPE html> <html> <head> <title>动态时钟</title> <style> body { background-color: #f1f1f1; } .clock { margin: 50px auto; width: 200px; height: 200px; border-radius: 50%; background-color: white; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1); position: relative; } .hour-hand { position: absolute; top: 50%; left: 50%; width: 6px; height: 50px; background-color: black; transform-origin: bottom center; transform: translateX(-50%) rotate(0deg); animation: hourAnim 43200s linear infinite; } .minute-hand { position: absolute; top: 50%; left: 50%; width: 4px; height: 80px; background-color: black; transform-origin: bottom center; transform: translateX(-50%) rotate(0deg); animation: minuteAnim 3600s linear infinite; } .second-hand { position: absolute; top: 50%; left: 50%; width: 2px; height: 100px; background-color: red; transform-origin: bottom center; transform: translateX(-50%) rotate(0deg); animation: secondAnim 60s linear infinite; } @keyframes hourAnim { from { transform: translateX(-50%) rotate(0deg); } to { transform: translateX(-50%) rotate(360deg); } } @keyframes minuteAnim { from { transform: translateX(-50%) rotate(0deg); } to { transform: translateX(-50%) rotate(360deg); } } @keyframes secondAnim { from { transform: translateX(-50%) rotate(0deg); } to { transform: translateX(-50%) rotate(360deg); } } .clock-center { position: absolute; top: 50%; left: 50%; width: 10px; height: 10px; background-color: black; border-radius: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <div class="clock"> <div class="hour-hand"></div> <div class="minute-hand"></div> <div class="second-hand"></div> <div class="clock-center"></div> </div> <script src="clock.js"></script> </body> </html> ``` JavaScript: ```javascript function updateClock() { const now = new Date(); const hour = now.getHours(); const minute = now.getMinutes(); const second = now.getSeconds(); const hourDegree = (hour % 12) * 30 + (minute / 60) * 30; const minuteDegree = minute * 6; const secondDegree = second * 6; document.querySelector(".hour-hand").style.transform = `translateX(-50%) rotate(${hourDegree}deg)`; document.querySelector(".minute-hand").style.transform = `translateX(-50%) rotate(${minuteDegree}deg)`; document.querySelector(".second-hand").style.transform = `translateX(-50%) rotate(${secondDegree}deg)`; } setInterval(updateClock, 1000); ``` 这个代码通过 `setInterval` 方法每秒更新一次时钟, `updateClock` 函数获取当前时间并计算出每个指针应该旋转的角度,然后通过设置 `transform` 属性来旋转指针。其中,时针每小时旋转30度,分针每分钟旋转6度,秒针每秒钟旋转6度。 在HTML文件中,我们定义了一个时钟的容器 `.clock`,并在其中添加了时针、分针、秒针和中心点。每个指针都有自己的类名和CSS样式,也有自己的动画效果。其中,时针的动画总时长为43200秒(12小时),分针的动画总时长为3600秒(1小时),秒针的动画总时长为60秒(1分钟)。这样可以让指针在一定的时间内完成一圈的旋转。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值