js 日期循环

JavaScript011

js 日期循环,第1张

不用这么麻烦的计算每个月多少天。

看看这个。

//日期增加函数

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)}

JS有自带的日期函数,获取如下:var date = new Date()var year = date.getFullYear()//获取年份var month = date.getMonth()+1//获取月份,因为是从0-11所以要+1var day = date.getDate()//获取天数var hour = date.getHours()//获取小时var minute = date.getMinutes()//获取分钟var second = date.getSeconds()//获取秒

给一个代码例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<form name="d_reg_form" id="d_reg_form" action="">

<select   name="year"   size="1"   onChange="return CheckDay()">   

                     <script language="javascript">   

                        for(i=2050i>=1940i--)   

                            {   

                                document.write('<option   value="'+i+'">'+i+'</option>')   

                            }   

                     </script>   

</select>年

<select name="month"   size="1"   onChange="return CheckDay()">   

                        <option   value="1">1</option>   

                        <option   value="2">2</option>   

                        <option   value="3">3</option>   

                        <option   value="4">4</option>   

                        <option   value="5">5</option>   

                        <option   value="6">6</option>   

                        <option   value="7">7</option>   

                        <option   value="8">8</option>   

                        <option   value="9">9</option>   

                        <option   value="10">10</option>   

                        <option   value="11">11</option>   

                        <option   value="12">12</option>   

</select>月  

<select   name="day"   size="1"></select>日

<script>

function CheckDay(){

    

    var   YearNo,MonthNo,DayNo   

    YearNo   =   document.d_reg_form.year.value   

    MonthNo   =   document.d_reg_form.month.value   

    var   i,j,DayCount   

    

    if(MonthNo==1 || MonthNo==3 || MonthNo==5 || MonthNo==7 || MonthNo==8 || MonthNo==10 || MonthNo==12)   

    {DayCount = 31}   

    

    if(MonthNo==4 || MonthNo==6 || MonthNo==9 || MonthNo==11 )

    {DayCount = 30}   

    

    if(MonthNo==2){   

           if(IsRunY(YearNo)){

               DayCount = 29

           }else{

               DayCount = 28

           }   

    }   

    

    document.d_reg_form.day.length=DayCount   

    for (i=0i<=DayCount-1i++)   

    {   

       document.d_reg_form.day[i]=new Option(i+1)     

       document.d_reg_form.day[i].value=i+1     

    }   

}

function IsRunY(YearNo)   

{   

    // 能被4整除却不能被100整除 或能被400整除的年份

    // 则是闰年

    if ((YearNo % 4 == 0 && YearNo % 100 != 0) || YearNo % 400 == 0){

        return true

    }else{

        return false

    }

}

CheckDay()

</script>

</form>

</body>

</html>