日期:2014-05-16  浏览次数:20308 次

js 年月日星期

//年月日,星期
function getDate(){
    var arr="日一二三四五六".split("");
    var d=new Date();
    var year= d.getFullYear();
    var month= d.getMonth()+1;
    var day= d.getDate();
    var tag="";
    var date = new Date(year, month, 0);//按年月
    var monDayCount=date.getDate();//本年月总天数
    var dayCount =day+7;//可推迟7天
    var newDay=0;//新天
    var newM;//新月
    var sNewM;
    var newYear=year+1;//新年
    var newYearMon=1;//新一年下的新月
    var newYearDay=0;//新一年下的新日

    var m=parseInt(month,10);
    var sM= m.toString().length<2?"0"+m:m;//补0
    for(var i=day;i<=dayCount;i++){
        if(i<=monDayCount){ //如果没有超过当月总天数
            i= i.toString().length<2?"0"+i:i;//补0
            tag+="<option value="+year+"-"+sM+"-"+i+" day=星期"+arr[new Date(year,m-1,i).getDay()]+">"+year+"-"+sM+"-"+i+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;星期"+arr[new Date(year,m-1,i).getDay()]+"</option>"
        }
        else{ //月和天数都+1
             newDay=parseInt(newDay,10)+1;
             newM=parseInt(month,10)+1;
            if(newM<=12){ //判断是否超过12个月,如果是则年数+1
                newDay= newDay.toString().length<2?"0"+newDay:newDay;//补0
                sNewM= newM.toString().length<2?"0"+newM:newM;//补0
                tag+="<option value="+year+"-"+sNewM+"-"+newDay+" day=星期"+arr[new Date(year,newM-1,newDay).getDay()]+">"+year+"-"+sNewM+"-"+newDay+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;星期"+arr[new Date(year,newM-1,newDay).getDay()]+"</option>"
            }
            else{
                newYearDay=newYearDay+1;
                tag+="<option value="+newYear+"-"+newYearMon+"-"+newYearDay+" day=星期"+arr[new Date(newYear,newYearMon-1,newYearDay).getDay()]+">"+newYear+"-"+newYearMon+"-"+newYearDay+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;星期"+arr[new Date(newYear,newYearMon-1,newYearDay).getDay()]+"</option>"
            }

        }

    }
    $("#mealDate").append(tag);
}
?