1. 공공데이터포털 API 사용
const getHolidays = async (year) => {
const serviceKey = '인증키';
const url = `http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getHoliDeInfo?serviceKey=${serviceKey}&solYear=${year}&numOfRows=100`;
try {
const response = await axios.get(url);
const holidays = {};
response.data.response.body.items.item.forEach(item => {
const date = item.locdate.toString();
const formattedDate = `${date.substring(0,4)}-${date.substring(4,6)}-${date.substring(6,8)}`;
holidays[formattedDate] = item.dateName;
});
return holidays;
} catch (error) {
console.error('공휴일 정보 조회 실패:', error);
return {};
}
};
2. 구글 캘린더 API 사용
const getHolidays = async (year) => {
const calendarId = 'ko.south_korea#holiday@group.v.calendar.google.com';
const apiKey = 'API_키';
const url = `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}&timeMin=${year}-01-01T00:00:00Z&timeMax=${year}-12-31T23:59:59Z`;
try {
const response = await axios.get(url);
const holidays = {};
response.data.items.forEach(item => {
const date = item.start.date;
holidays[date] = item.summary;
});
return holidays;
} catch (error) {
console.error('공휴일 정보 조회 실패:', error);
return {};
}
};
3. 외부 라이브러리 사용
ex) holiday-kr
import holidays from 'holiday-kr';
const holidays = getHolidays(new Date().getFullYear());
- 대체공휴일 표시 안 됨
'vue.js' 카테고리의 다른 글
[vue.js] 컴포넌트 심화 (0) | 2025.01.08 |
---|---|
[vue.js] 컴포넌트 기초 (0) | 2025.01.08 |
[vue.js] 페이지를 이동하면서 데이터를 넘기는 방법 (0) | 2024.11.12 |
[vue.js] Vue Router 설정 (1) | 2024.11.10 |
[Vue.js]Vue CLI로 Vue 프로젝트 생성 (0) | 2024.11.10 |