Skip to content

Instantly share code, notes, and snippets.

@leegeunhyeok
Created October 8, 2020 04:06
Show Gist options
  • Save leegeunhyeok/8695aaf29674b098b7da7696e90810bb to your computer and use it in GitHub Desktop.
Save leegeunhyeok/8695aaf29674b098b7da7696e90810bb to your computer and use it in GitHub Desktop.
📅 특정 날짜 기준으로 당월 N주차 구하기
/**
* 특정 날짜 기준으로 당월 N주차 구하기
* @author dev.ghlee@gmail.com
*/
/**
* 지정된 날짜가 몇 주차인지 계산하여 반환합니다
* @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
@leegeunhyeok
Copy link
Author

일요일이 한 주의 시작 요일임을 기준으로 함

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment