日付を入力してください。
<p><input type="date" id="date"></p> <p id="dateResult">日付を入力してください。</p>
const calendarId = 'ja.japanese#holiday@group.v.calendar.google.com'
const calendarApiUrl = 'https://www.googleapis.com/calendar/v3/calendars/'
const apiKey = 'AIzaSyBPACe2aM5lHUx0-6L0zUss0yl_o6RQsMw'
function loadGoogleCalendarApi() {
gapi.client.init({
apiKey: apiKey,
}).then(() => {
return gapi.client.request({
path: calendarApiUrl + encodeURIComponent(calendarId) + '/events'
})
}).then((res) => {
const items = (res.result.items).reduce((a, c) => {
a.push({date: c.start.date, summary: c.summary})
return a
}, []).sort((a, b) => {
if (new Date(a.date) > new Date(b.date)) {
return 1
} else if (new Date(a.date) < new Date(b.date)) {
return -1
} else {
return 0
}
})
console.table(items)
// 祝日判定
const date = document.getElementById('date')
const dateResult = document.getElementById('dateResult')
date.addEventListener('input', (date) => {
const summary = items.filter(v => v.date === date.target.value)
const formatter = new Intl.DateTimeFormat('ja-JP', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
})
if (summary.length) {
dateResult.textContent = `${formatter.format(new Date(date.target.value))}は祝日です。(${summary[0].summary})`
} else {
dateResult.textContent = `${formatter.format(new Date(date.target.value))}は祝日ではありません。`
}
})
})
}
gapi.load('client', loadGoogleCalendarApi)