看看这个。
//日期增加函数
function dateAdd(strInterval, NumDay, dtDate) {
var dtTmp = new Date(dtDate)
if (isNaN(dtTmp)) dtTmp = new Date()
switch (strInterval) {
case "s":return new Date(Date.parse(dtTmp) + (1000 * NumDay))
case "n":return new Date(Date.parse(dtTmp) + (60000 * NumDay))
case "h":return new Date(Date.parse(dtTmp) + (3600000 * NumDay))
case "d":return new Date(Date.parse(dtTmp) + (86400000 * NumDay))
case "w":return new Date(Date.parse(dtTmp) + ((86400000 * 7) * NumDay))
case "m":return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + NumDay, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds())
case "y":return new Date((dtTmp.getFullYear() + NumDay), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds())
}
}
//将日期beginDate加上一天
//strDateList = dateAdd("d", 1, new Date()).toLocaleString()
strDateList = dateAdd("d", 1, '2012/09/28').toLocaleString()
另赠送你一个函数:
//| 求两个时间的天数差 日期格式为 YYYY-MM-dd
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'))
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1)
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'))
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'))
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1)
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'))
var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000)
return Math.abs(cha)}
下面是代码示例:方法1:
$(function(){
var
handler
=
function(){
}
var
timer
=
setInterval(
handler
,
5000)
//每5秒调用一次方法
var
clear
=
function(){
clearInterval(timer)
//清理时间
})
方法2:
function
methodJS(){
alert("你好!我是方法!")
}
function
Start(){
setInterval(methodJS,
5000)
}
setTimeout(Start,
7000)//延迟7秒开始周期回调
方法3:
window.onload
=
function(){
setTimeout(fun,5000)
}
function
Start(){
//这里是你要执行的方法!
}
<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8"><script type="text/javascript">
var date1 = new Date()
time1 = Date.parse(date1)
date1.setDate(date1.getDate()+1)
date1.setUTCHours('-8','0','0')
time2 = Date.parse(date1)
var ok = 1
while(ok){
time1 = time1 + 1000*60*20
if(time1 >time2){
ok=0
}else{
date2 = new Date(time1)
document.write(date2.getHours()+'时'+date2.getMinutes()+'分'+date2.getSeconds()+'秒<br>')
}
}
</script>