js获取星期的方法

JavaScript0621

js获取星期的方法,第1张

<script type="text/javascript" language="javascript">

function calc(){

var date=new Date(),str

week=date.getDay()

if(week==0){

str="星期日"

}else if(week==1){

str="星期一"

}else if(week==2){

str="星期二"

}else if(week==3){

str="星期三"

}else if(week==4){

str="星期四"

}else if(week==5){

str="星期五"

}else if(week==6){

str="星期六"

}

alert(str)

}

</script>

注意我用的是alert对话框输出的值,new Date(),括号里面可以填指定日期,格式是:YYYY-MM-DD,注意加引号,如果new Date()后面的括号什么都不填,表示获取当前系统的日期和时间,date.getDay()这方法返回的是一个0到6之间的整数,0表示星期天,1到6表示星期一到星期六

方法一:

// 计算系统当前是星期几var str = "今天是星期" + "日一二三四五六".charat(new Date().getDay())

方法二:

var a = new array("日", "一", "二", "三", "四", "五", "六")var week = new Date().getDay()var str = "今天是星期"+ a[week]

alert(str)

方法三:

var str = "今天是星期"var week = new Date().getDay()switch (week) {case 0 :

str += "日" break case 1 :

str += "一" break case 2 :

str += "二" break case 3 :

str += "三" break case 4 :

str += "四" break case 5 :

str += "五" break case 6 :

str += "六" break

}

alert(str)

方法四:

var str = ""var week = new Date().getDay()if (week == 0) {

str = "今天是星期日"

} else if (week == 1) {

str = "今天是星期一"

} else if (week == 2) {

str = "今天是星期二"

} else if (week == 3) {

str = "今天是星期三"

} else if (week == 4) {

str = "今天是星期四"

} else if (week == 5) {

str = "今天是星期五"

} else if (week == 6) {

str = "今天是星期六"

}

alert(str)

一般一星期顺序如下:

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

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