如何在按钮上显示星期几?

huangapple go评论71阅读模式
英文:

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.

&lt;button class=&quot;button&quot;&gt;Conquer Monday&lt;/button&gt;

</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(&quot;dayButton&quot;);
  // array of day
  const weekdays = [&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;];
  //Indice day
  const currentDayiIndex = new Date().getDay();
  //Show day in click the button
    dayButton.addEventListener(&quot;click&quot;,()=&gt;{
        day.innerHTML = weekdays[currentDayiIndex]
        button.innerHTML = &quot;Conquer &quot; + weekdays[currentDayiIndex];
        })

<!-- language: lang-html -->

&lt;button class=&quot;button&quot; id=&quot;dayButton&quot;&gt;Show Day&lt;/button&gt;
&lt;p id=&quot;day&quot;&gt;&lt;/p&gt;

<!-- 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(&#39;.button&#39;).textContent = &#39;Conquer &#39; + new Date().toLocaleDateString(&#39;en-us&#39;, {weekday:&#39;long&#39;});

<!-- language: lang-html -->

&lt;button class=&quot;button&quot;&gt;&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 20:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定