英文:
How to display the day of the week on a button?
问题
"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>
英文:
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>
</details>
# 答案1
**得分**: 1
Sure, here's the translated code part:
```javascript
//获取具有id的按钮
const button = document.getElementById("dayButton");
//一周的日期数组
const weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
//当前日期的索引
const currentDayIndex = new Date().getDay();
//在点击按钮时显示日期
dayButton.addEventListener("click", () => {
day.innerHTML = weekdays[currentDayIndex]
button.innerHTML = "征服 " + weekdays[currentDayIndex];
})
And here's the translated HTML part:
<button class="button" id="dayButton">显示日期</button>
<p id="day"></p>
I hope this helps!
英文:
can you using this code js using Date().getDay()
return day of week between 0 and 6 and weekdays
list of day of week
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
//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];
})
<!-- language: lang-html -->
<button class="button" id="dayButton">Show Day</button>
<p id="day"></p>
<!-- end snippet -->
I hope I helped you
答案2
得分: 1
你可以使用 Date::toLocaleDateString
来显示当前星期几:
document.querySelector('.button').textContent = '征服 ' + new Date().toLocaleDateString('en-us', {weekday:'long'});
<button class="button"></button>
英文:
You could use Date::toLocaleDateString
to display the current week day:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.querySelector('.button').textContent = 'Conquer ' + new Date().toLocaleDateString('en-us', {weekday:'long'});
<!-- language: lang-html -->
<button class="button"></button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论