---
title: "JS 倒计时代码"
author: "MVC2008MVC"
date: "2011-03-13T14:55:08Z"
tags: ["prototype"]
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2011-03-13T14:55:08Z"
id: 960246
url: "https://mvc2008mvc.iteye.com/blog/960246"
---

使用示例: \[code="java"] var tim = new TimeLeft(); tim.setServerDate("2011-03-2 8:34:45"); //设置当前时间 tim.setEndDate("2011-03-2 8:55:45"); //设置结束日期 tim.setDivId("LiveClock1"); tim.clockStart();\[/code] \[code="java"]/\*\** 时间倒计时函数 \*\*/ function TimeLeft(){ this.nDate = null; this.eDate = null; this.divId = null; this.DifferHour = -1; this.DifferMinute = -1; this.DifferSecond = -1; this.sec\_0 = 0; //要加的秒数 this.min\_0 = 0; //分钟 this.hor\_0 = 0; //小时 this.day\_0 = 0; //天 this.moth\_0 =0; //月 this.yea\_0 = 0; //年 this.sec = 0; //要加的秒数 this.min = 0; //分钟 this.hor = 0; //小时 this.day = 0; //天 this.moth =0; //月 this.yea = 0; //年 //--------剩余时间参数---------- this.leftDay = 0; this.leftHour=0; this.leftMin=0; this.leftSec=0; //-----------定时器使用---------- this.sid="TimeLeft\_ASBKC"; eval(this.sid+"=this"); this.divStr='0'; } TimeLeft.prototype.setServerDate=function(serverDate){ //设置当前时间 格式: 2011-03-2 12:34:45 var dts = serverDate.split(" "); var time1 = dts\[0].split("-"); var time2= dts\[1].split(":"); this.sec = parseInt(time2\[2]); //要加的秒数 this.min = parseInt(time2\[1]); //分钟 this.hor = parseInt(time2\[0]); //小时 this.day = parseInt(time1\[2]); //天 this.moth =parseInt(time1\[1]); //月 this.yea = parseInt(time1\[0]); //年 } TimeLeft.prototype.setEndDate=function(endDate){ //设置服务器时间 格式: 2011-07-2 12:34:45 var dts = endDate.split(" "); var time1 = dts\[0].split("-"); var time2 = dts\[1].split(":"); this.sec\_0 = parseInt(time2\[2]); //要加的秒数 this.min\_0 = parseInt(time2\[1]); //分钟 this.hor\_0 = parseInt(time2\[0]); //小时 this.day\_0 = parseInt(time1\[2]); //天 this.moth\_0 = parseInt(time1\[1]); //月 this.yea\_0 = parseInt(time1\[0]); //年 this.Tday1 = new Date(this.yea\_0,this.moth\_0,this.day\_0,this.hor\_0,this.min\_0,this.sec\_0); //\*\*倒计时时间-注意格式 this.daysms = 24 * 60 * 60 * 1000; this.hoursms = 60 * 60 * 1000; this.Secondms = 60 * 1000; this.microsecond = 1000; } //设置当前时间 年月日时分秒格式 TimeLeft.prototype.setServerDateFormat=function(year,month,day,hour,min,sec){ this.sec = sec; //要加的秒数 this.min = min; //分钟 this.hor = hour; //小时 this.day = day; //天 this.moth =month; //月 this.yea = year; //年 } //设置到期时间 年月日时分秒格式 TimeLeft.prototype.setEndDateFormat=function(year,month,day,hour,min,sec){ this.sec\_0 = sec; //要加的秒数 this.min\_0 = min; //分钟 this.hor\_0 = hour; //小时 this.day\_0 = day; //天 this.moth\_0 =month; //月 this.yea\_0 = year; //年 this.Tday1 = new Date(this.yea\_0,this.moth\_0,this.day\_0,this.hor\_0,this.min\_0,this.sec\_0); //\*\*倒计时时间-注意格式 this.daysms = 24 * 60 * 60 * 1000; this.hoursms = 60 * 60 * 1000; this.Secondms = 60 * 1000; this.microsecond = 1000; } TimeLeft.prototype.setDivId=function(v){ //设置要显示的Div this.divId=v; } TimeLeft.prototype.getDay=function(){ //剩余天数 return this.leftDay; } TimeLeft.prototype.getHour=function(){ //剩余小时 return this.leftHour; } TimeLeft.prototype.getMin=function(){ //剩余分钟 return this.leftMin; } TimeLeft.prototype.getSec=function(){ //剩余秒数 return this.leftSec; } TimeLeft.prototype.showTimes=function(){ //显示到页面 var timeStr =""+ this.getDay()+"天"+this.getHour()+"小时"+this.getMin()+"分"+this.getSec()+"秒"+""; if(this.divStr=='0'){ var mystr = document.getElementById(this.divId).innerText; this.divStr = mystr; } document.getElementById(this.divId).innerHTML=this.divStr + timeStr; } TimeLeft.prototype.clockStart=function(){ //时间开始 this.sec = this.sec + 1; if(this.sec&gt;=60){ this.sec = 0; this.min = this.min +1; } if(this.min&gt;=60){ this.min = 0; this.hor = this.hor+1; } if(this.hor&gt;=23){ this.hor = 1; this.day = this.day+1; } //-----如果比较和时间当前时间相同则停止 var time = new Date(this.yea,this.moth,this.day,this.hor,this.min,this.sec); var hour = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); var timevalue = ""+((hour &gt; 12)?hour-12:hour); timevalue +=((minute &lt; 10) ? ":0":":")+minute; timevalue +=((second &lt; 10) ? ":0":":")+second; timevalue +=((hour &gt;12 ) ? " PM":" AM"); var convertHour = this.DifferHour; var convertMinute = this.DifferMinute; var convertSecond = this.DifferSecond; var Diffms = this.Tday1.getTime() - time.getTime(); this.DifferHour = Math.floor(Diffms / this.daysms); Diffms -= this.DifferHour * this.daysms; this.DifferMinute = Math.floor(Diffms / this.hoursms); Diffms -= this.DifferMinute * this.hoursms; this.DifferSecond = Math.floor(Diffms / this.Secondms); Diffms -= this.DifferSecond * this.Secondms; var dSecs = Math.floor(Diffms / this.microsecond); //设置当前剩余的时间数 if(this.DifferHour&lt;0){ //没有剩余时间 this.leftDay = 0; }else{ this.leftDay = this.DifferHour; this.leftHour = this.DifferMinute; this.leftMin = this.DifferSecond; this.leftSec = dSecs; } this.showTimes(); //调用更新页面方法 setTimeout(this.sid+".clockStart();",1000); }\[/code]