// 年・月・日を取得
const getDate = new Date();
const year = getDate.getFullYear();
const month = ('0' + (getDate.getMonth() + 1)).slice(-2);
const day = getDate.getDate();
const today = document.querySelector('#today a');
today.insertAdjacentHTML('afterbegin', ${year}-${month}-${day}
);
// 時・分・曜日を取得
const getTime = new Date();
const hour = getTime.getHours();
const minute = getTime.getMinutes();
const week = getDate.getDay();
const yobi = new Array("日","月","火","水","木","金","土");
const time = document.getElementById('time');
time.insertAdjacentHTML('afterbegin', ${hour}:${minute}(${yobi[week]})
);
cf
日付の比較
window.onload = function () {
const today = new Date();
const past = new Date('2020/4/1 12:30:45');
const future = new Date('2023/4/1 12:30:45');
console.log(today); // Wed May 11 2022 23:52:54 GMT+0900 (日本標準時)
console.log(past); // Wed Apr 01 2020 12:30:45 GMT+0900 (日本標準時)
console.log(future); // Sat Apr 01 2023 12:30:45 GMT+0900 (日本標準時)
console.log("today > past = " + (today > past)); // today > past = true
console.log("today < past = " + (today < past)); // today < past = false
console.log("today > future = " + (today > future)); // today > future = false
console.log("today < future = " + (today < future)); // today < future = true
};