簡體   English   中英

如何使用 javascript 根據星期幾顯示文本消息?

[英]How can I display a text message based on the day of the week using javascript?

我正在嘗試使用 JavaScript 在 HTML 文件中顯示基於星期幾的消息/文本。 當談到 JavaScript 時,我不是專家,我一直在用這個打敗自己。 這是我迄今為止所擁有的

    var specials;
    var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

    if (days == Monday) {
      specials = "$4.00 Burgers All Day";
    } 
    
    if (days == Tuesday) {
      specials = "$1.25 Tacos All Day";
    }

    if (days == Wednesday) {
        specials = "$0.70 Wings All Day";
    }

    if (days == Thursday) {
        specials = "$6.49 Steak or Chicken Philly";
    }

    if (days == Friday) {
        specials = "";
    }
    if (days == Saturday) {
        specials = "";
    }
    if (days == Sunday) {
        specials = "";
    }
    document.getElementById("specials").innerHTML = specials;

任何幫助是極大的贊賞。 謝謝!

使用Date object 您可以獲取當前日期/時間,然后像這樣從中獲取日期。

const currentDay = new Date(Date.now()).getDay();

它映射到周日到周六的 0 到 6。 這只是您數組中的索引。

days[0]; //Sunday

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects /日期/現在

你可以這樣做:

 let specials; const currentDay = new Date().getDay(); // from sunday to saturday switch(currentDay) { case 0: specials = ''; break; case 1: specials = '$4.00 Burgers All Day'; break; case 2: specials = '$1.25 Tacos All Day'; break; case 3: specials = '$0.70 Wings All Day'; break; case 4: specials = '$6.49 Steak or Chicken Philly'; break; case 5: specials = ''; break; case 6: specials = ''; break; } document.getElementById("specials").innerHTML = specials;
 <h2 id="specials"></h2>

var specials;
    var day = "Monday" /* <-- for Example */

    // "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"

    if (day == "Monday") {
      specials = "$4.00 Burgers All Day";
    } 
    
    if (day == "Tuesday") {
      specials = "$1.25 Tacos All Day";
    }

    if (day == "Wednesday") {
        specials = "$0.70 Wings All Day";
    }

    if (day == "Thursday") {
        specials = "$6.49 Steak or Chicken Philly";
    }

    if (day == "Friday") {
        specials = "";
    }
    if (day == "Saturday") {
        specials = "";
    }
    if (day == "Sunday") {
        specials = "";
    }
    document.getElementById("specials").innerHTML = specials;

使用 getDay 和數組

 var specials = [ "foo", "bar", "baz", "goober", "funky", "chicken", "dinner" ]; var d = new Date(); var dayOfWeek = d.getDay(); document.getElementById("out").textContent = specials[dayOfWeek];
 <div id="out"></div>

如果你不介意使用 momentjs。 您需要創建今天的日期,並從今天的變量中獲取日期,使用 switch 語句來獲取您的值,而且由於周五到周日的值相同,只需使用默認情況。

    var specials;
    var today=moment();
    var day= today.day();
    console.log("day",day)
    switch(day){
    case 1:
     specials = "$0.70 Wings All Day";
    break;
    case 2:
     specials = "$1.25 Tacos All Day";
    break;
      case 3:
       specials = "$0.70 Wings All Day";
    break;
      case 4:
        specials = "$6.49 Steak or Chicken Philly";
    break;
     default:
         specials = "";
    break;
    }
console.log(specials)

但是伙計們,使用 object,這是最簡單的,可能也是最快的。 沒有邏輯。

 const days = { "Sunday": "", "Monday": "$4.00 Burgers All Day", "Tuesday": "$1.25 Tacos All Day", "Wednesday": "$0.70 Wings All Day", "Thursday": "$6.49 Steak or Chicken Philly'", "Friday": "", "Saturday": "" } /* Example monday */ console.log( days["Monday"] )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM