Created
March 19, 2019 15:51
-
-
Save techouse/3086282471805942b7abecb98fe7497f to your computer and use it in GitHub Desktop.
Get the week of month using date-fns
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
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