JS如何利用日期判断星期几

JavaScript027

JS如何利用日期判断星期几,第1张

一般一星期顺序如下:

星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"

function getMyDay(date){

var week

if(date.getDay()==0) week="周日"

if(date.getDay()==1) week="周一"

if(date.getDay()==2) week="周二"

if(date.getDay()==3) week="周三"

if(date.getDay()==4) week="周四"

if(date.getDay()==5) week="周五"

if(date.getDay()==6) week="周六"

return week

}

var w1 = getMyDay(new Date("2015-7-12"))

var date = new Date()

date.setDate(date.getDate()-date.getDay()+1)

$("#year option:contains("+date.getFullYear()+")").prop("selected",true)

$("#month option").eq(date.getMonth()+1).prop("selected",true)

$(#day option").eq(date.getDate()).prop("selected",true)

date.setDate(date.getDate()+7)

$("#year1 option:contains("+date.getFullYear()+")").prop("selected",true)

$("#month1 option").eq(date.getMonth()+1).prop("selected",true)

$(#day1 option").eq(date.getDate()).prop("selected",true)

顺便说一句:现在通常的做法(也是国际惯例),是把星期日作为一周的开始,而星期六则是周末,如果你想这么做,只需把第二行后面的+1去掉即可。