Dimitri
Dimitri

Reputation: 15

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

I'm trying to display a message/text based on the day of the week using JavaScript into a HTML file. I'm not an expert when it comes to JavaScript and I have been beating myself up with this. This what I have thus far

    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;

Any help is greatly appreciated. Thank you!

Upvotes: 0

Views: 1167

Answers (6)

Carson Wood
Carson Wood

Reputation: 1314

Using the Date object you can get the current Date/time and then get the day from it like so.

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

It maps to 0 through 6, Sunday through Saturday. Which would just be the indices in your array.

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/Date/now

Upvotes: 2

NVRM
NVRM

Reputation: 13082

But guys, use an object! This is the simplest, and probably the fastest, there is no logic.

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

Upvotes: 0

Nonik
Nonik

Reputation: 655

if you don't mind using momentjs. You need to create today's date, and get day out of today's variable, use switch statement to get your values, also since friday thru sun values are same, just use default case.

    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)

Upvotes: 0

epascarello
epascarello

Reputation: 207511

Using getDay and an array

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>

Upvotes: 0

Mahdi Sheibak
Mahdi Sheibak

Reputation: 758

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;

Upvotes: 0

Namysh
Namysh

Reputation: 4627

You can do this :

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>

Upvotes: 0

Related Questions