实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>2019春节倒计时</title> <style> .main{ width: 800px; height: 400px; background-color: plum; margin:0 auto; } p{ color:mediumblue; font-size: 30px; text-align: center; line-height: 300px; } </style> </head> <body> <div class="main"> <p id="p1"></p> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script> $(document).ready(function(){ var timer = null; timer = setInterval(function () { //clearInterval(timer); var now = parseInt((new Date()).getTime()/1000); var off = DateToUnix("2019-2-4 0:0:0") - now; var day = parseInt(off/86400); var hours = parseInt(off%86400/3600); var min = parseInt(off%3600/60); var sec = parseInt(off%60); var str = day+"天"+hours+"时"+min+"分"+sec+"秒"; $("#p1").text ('距离2019年春节,还有'+ str);//+ str; function DateToUnix(string) { var f = string.split(' ', 2); var d = (f[0] ? f[0] : '').split('-', 3); var t = (f[1] ? f[1] : '').split(':', 3); return (new Date( parseInt(d[0], 10) || null, (parseInt(d[1], 10) || 1) - 1, parseInt(d[2], 10) || null, parseInt(t[0], 10) || null, parseInt(t[1], 10) || null, parseInt(t[2], 10) || null )).getTime() / 1000; } //clearInterval(timer); }, 1000); }) </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例