Skip to content

Instantly share code, notes, and snippets.

@techouse
Created March 19, 2019 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techouse/3086282471805942b7abecb98fe7497f to your computer and use it in GitHub Desktop.
Save techouse/3086282471805942b7abecb98fe7497f to your computer and use it in GitHub Desktop.
Get the week of month using date-fns
import {
differenceInDays,
startOfMonth,
startOfWeek,
getDate
} from 'date-fns'
const weekOfMonth = function (date) {
const firstDayOfMonth = startOfMonth(date)
const firstDayOfWeek = startOfWeek(firstDayOfMonth)
const offset = differenceInDays(firstDayOfMonth, firstDayOfWeek)
return Math.ceil((getDate(date) + offset) / 7)
}
export default weekOfMonth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment