前提
時間で表示が変わるようにしたいです。
実現したいこと
「○○時は〇〇です」と時間によって表示を変えたい。
該当のソースコード
javascript
'use strict'; const now = new Date(); const year = now.getFullYear(); const month = now.getMonth(); const date = now.getDate(); const hour = now.getHours(); const min = now.getMinutes(); let ampm = ''; if(hour < 12) { ampm = '午前'; } else { ampm = '午後'; } const output = `${year}/${month + 1}/${date} ${ampm}${hour % 12}:${min}`; document.getElementById('time').textContent = output; /*const gatieria = `${hour}時はガチエリアです`; const gatiyagura = `${hour}時はガチヤグラです`; */ /* document.getElementById('now').textContent = nowgame; */ if (3 <= hour && hour <= 5 || 15 <= hour <= 17){ document.getElementById('nowgame').textContent = `${hour}時はガチホコバトルです`; document.write( '<img src="gatihoko.png">' ); }else if(6 <= hour && hour <= 8 ){ document.getElementById('nowgame').textContent = `${hour}時はガチエリアです`; document.write( '<img src="gatieria.png">' ); }else if(9 <= hour && hour <= 11 ) { document.getElementById('nowgame').textContent = `${hour}時はガチヤグラです`; document.write( '<img src="gatiyagura.png">' ); }else if(12 <= hour && hour <= 14 ) { document.getElementById('nowgame').textContent = `${hour}時はガチアサリです`; document.write( '<img src="gatiasari.png">' ); }
試したこと
||などの比較演算子でhourを比較。
何時になっても同じ表示になってしまう。
hourを秒単位に直さないといけないでしょうか。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/09/24 00:45