Home > Web Front-end > JS Tutorial > 4 ways to write JS code for what day of the week today is_javascript skills

4 ways to write JS code for what day of the week today is_javascript skills

WBOY
Release: 2016-05-16 17:22:21
Original
1450 people have browsed it

The first way to write

Copy code The code is as follows:

var str = "" ;
var week = new Date().getDay();
if (week == 0) {
str = "Today is Sunday";
} else if (week == 1) {
str = "Today is Monday";
} else if (week == 2) {
str = "Today is Tuesday";
} else if (week == 3) {
str = "Today is Wednesday";
} else if (week == 4) {
str = "Today is Thursday";
} else if (week == 5) {
        str = "Today is Friday";
} else if (week == 6) {
        str = "Today is Saturday"; > alert(str);


The second writing method


var str = "Today is week";
var week = new Date().getDay();
switch (week) {
case 0 :
str = "日";
break;
case 1 :
str = "一";
break;
case 2:
str = "二";
               break;             str = "四";                                                        :
str = "five";
break;
case 6 :
str = "six";
break;
}
alert(str);


The third way of writing




Copy code

The code is as follows:

var a = new Array("日", "一", "二", "三", "四", "五", "六"); var week = new Date().getDay(); var str = "Today is week" a[week]; alert(str); The fourth way of writing



Copy the code

The code is as follows:

var str = "Today is week" "Day one, two, three, four, five, six".charAt(new Date().getDay( ));alert(str);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template