skip to Main Content

Is it possible to automatically display the day of the week on a button?

See the markup below, I need to make "Monday" show whatever day of the week it is.

<button class="button">Conquer Monday</button>

2

Answers


  1. can you using this code js using Date().getDay() return day of week between 0 and 6 and weekdays list of day of week

    //get button with id
      const button = document.getElementById("dayButton");
      // array of day
      const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
      //Indice day
      const currentDayiIndex = new Date().getDay();
      //Show day in click the button
        dayButton.addEventListener("click",()=>{
            day.innerHTML = weekdays[currentDayiIndex]
            button.innerHTML = "Conquer " + weekdays[currentDayiIndex];
            })
    <button class="button" id="dayButton">Show Day</button>
    <p id="day"></p>

    I hope I helped you

    Login or Signup to reply.
  2. You could use Date::toLocaleDateString to display the current week day:

    document.querySelector('.button').textContent = 'Conquer ' + new Date().toLocaleDateString('en-us', {weekday:'long'});
    <button class="button"></button>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search