Created
October 8, 2020 04:06
-
-
Save leegeunhyeok/8695aaf29674b098b7da7696e90810bb to your computer and use it in GitHub Desktop.
📅 특정 날짜 기준으로 당월 N주차 구하기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 특정 날짜 기준으로 당월 N주차 구하기 | |
* @author [email protected] | |
*/ | |
/** | |
* 지정된 날짜가 몇 주차인지 계산하여 반환합니다 | |
* @param {Date} dateFrom 주차 계산을 위한 기준 날짜 | |
*/ | |
const getWeekNumber = (dateFrom = new Date()) => { | |
// 해당 날짜 (일) | |
const currentDate = dateFrom.getDate(); | |
// 이번 달 1일로 지정 | |
const startOfMonth = new Date(dateFrom.setDate(1)); | |
// 이번 달 1일이 무슨 요일인지 확인 | |
const weekDay = startOfMonth.getDay(); // 0: Sun ~ 6: Sat | |
// ((요일 - 1) + 해당 날짜) / 7일로 나누기 = N 주차 | |
return parseInt(((weekDay - 1) + currentDate) / 7) + 1; | |
} | |
getWeekNumber(new Date()); // 2020-10-08 기준, 2 | |
getWeekNumber(new Date('2020-01-28')); // 5 |
Author
leegeunhyeok
commented
Oct 8, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment