Skip to content

Instantly share code, notes, and snippets.

@cometkim
Created February 6, 2019 17:26
Show Gist options
  • Save cometkim/9b52c68c338bd2b886816c9c07e07508 to your computer and use it in GitHub Desktop.
Save cometkim/9b52c68c338bd2b886816c9c07e07508 to your computer and use it in GitHub Desktop.
특정 날짜가 그 달의 몇 번 째 주차인지 계산하기
const format = require('date-fns/format')
const startOfWeek = require('date-fns/start_of_week')
const today = new Date()
// 시작일이 포함되는 주의 수요일(3)의 포함 달을 기준으로 주차를 계산
const baseDate = startOfWeek(today, { weekStartsOn: 3 })
const baseMonth = baseDate.getMonth() + 1
const baseDays = baseDate.getDate() + 1
const weekOfMonth = Math.ceil(baseDays / 7)
console.log(`${format(today, 'YYYY년 MM월 DD일')}은 ${baseMonth}월 ${weekOfMonth}주차 입니다.`)
@cometkim
Copy link
Author

cometkim commented Feb 6, 2019

Shorthand:

const weekOfMonth = d => ~~((require('date-fns/start_of_week')(d,{weekStartsOn:3}).getDate()+1)/7)+1

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