different scripting (recommended) _javascript tips for the day of the week

Source: Internet
Author: User
Tags case statement switch case

In the front-end field, a lot of times, a simple function can have many different ways to implement, today, take the day of the different script implementation as an example, hoping to inspire children's shoes more ideas.

1. Use if statement:

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";
}

2. Use the switch Case statement:

var str1 = "Today is the Week";
var week = new Date (). Getday ();
Switch (week) {case
    0:
        str1 + = "Day";
        break;
    Case 1:
        str1 = "one";
        break;
    Case 2:
        str1 + = "two";
        break;
    Case 3:
        str1 + = "three";
        break;
    Case 4:
        str1 + = "four";
        break;
    Case 5:
        str1 = "Five";
        break;
    Case 6:
        str1 = "Six";
        break;
alert (STR1);

3, artifact, a word to solve:

var str = "Today is the Week" + "Day 123456". CharAt (New Date (). Getday ());
alert (str);

The above this week several different script writing (recommendation) is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.