JS倒计时效果

Date()函数

var myDate =new Date()
:返回当前的日期和时间

  • getDate() 查看Date对象并返回日期(1-31)
  • getDay() 返回星期几(0-6)
  • getHours()返回小时数(0-23)
  • getMinutes返回分钟数(0-59)
  • getMonth()返回月份值(从0开始,+1)
  • getSeconds()返回秒数
  • getTime() 返回毫秒数
  • getYear() 返回年份
  • getFullYear() 返回年份(如2014)

    显示当前系统时间

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>当前系统时间</title>
    <link rel="stylesheet" href="style.css" />
    <script language="javascript" type="text/javascript">
    window.onload = function(){
    showTime();
    }
    function checkTime(i){ //补位处理
    if(i<10) {
    i='0'+i;
    }
    return i;
    }
    function showTime(){
    var now=new Date();
    var year= now.getFullYear() ;
    var month= now.getMonth()+1 ;
    var day= now.getDate() ;
    var h= now.getHours() ;
    var m= now.getMinutes() ;
    var s= now.getSeconds() ;
    m=checkTime(m)
    s=checkTime(s)

    var weekday=new Array(7)
    weekday[0]="星期日"
    weekday[1]="星期一"
    weekday[2]="星期二"
    weekday[3]="星期三"
    weekday[4]="星期四"
    weekday[5]="星期五"
    weekday[6]="星期六"

    document.getElementById("show").innerHTML=""+year+"年"+month+"月"+day+"日 "+ weekday[now.getDay()] +h+":"+m+":"+s;
    t=setTimeout('showTime()',500)
    }
    </script>

    </head>
    <body>
    <div class="content1">
    <div id="show">显示时间的位置</div>
    </div>
    </body>
    </html>

高考倒计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>高考考试时间</title>
<link rel="stylesheet" href="style.css" />
</head>
<script language="javascript" type="text/javascript">
window.onload = function(){
  var timedate= new Date('2016,6,6'); //自定义结束时间
  var now = new Date() ; //获取当前时间
  var date = timedate.getTime() - now.getTime(); //得出的为毫秒
  var time = Math.ceil(date/(1000*60*60*24)) ; //1000 * 60 * 60 * 24一天的秒数
if(time > 0 ){
document.getElementById('timeShow').innerHTML = time;
}
}
</script>

</head>
<body>
<div class="content2">
<div class="txtshow">距离高考还有<span id="timeShow"></span></div>
</div>
</body>
</html>

限时抢购(团购)倒计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>团购——限时抢</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="content3">
<div class="time">还剩 <span id="LeftTime"></span></div>
</div>
<script>
function FreshTime(){
var endtime=new Date("2014/5/15,12:20:12");//结束时间
var nowtime = new Date();//当前时间
var lefttime= parseInt((endtime.getTime()-nowtime.getTime())/1000) ;
d= parseInt(lefttime/24/60/60);
h= parseInt((lefttime/(60*60))%24);
m= parseInt(lefttime/60%60);
s= parseInt(lefttime%60);
document.getElementById("LeftTime").innerHTML=d+"天"+h+"小时"+m+"分"+s+"秒";
if(lefttime<=0){
document.getElementById("LeftTime").innerHTML="团购已结束";
clearInterval(sh);
}
}
FreshTime();
var sh;
sh= setTimeout(FreshTime,500) ;
</script>

</body>
</html>
坚持原创技术分享,您的支持将鼓励我继续创作!