how to use the startofweek function from date-fns in javascript

To use the startOfWeek() function from the date-fns library in JavaScript, you first need to install the package. You can install it using npm:

index.tsx
npm install date-fns
21 chars
2 lines

Once you have installed the package, you can import the startOfWeek() function and use it in your code as follows:

import { startOfWeek } from 'date-fns'

const date = new Date()
const startOfWeekDate = startOfWeek(date)

console.log(startOfWeekDate)
136 chars
7 lines

In the example above, we first import the startOfWeek() function from the date-fns package. We then get the current date using new Date(). Finally, we use the startOfWeek() function to get the start of the week for the given date and log it to the console.

The startOfWeek() function can also take an optional options object as its second argument. The options object can be used to customize the start of the week. For example, to start the week on Monday instead of Sunday, you can pass the following options object:

const startOfWeekMonday = startOfWeek(date, { weekStartsOn: 1 })
65 chars
2 lines

gistlibby LogSnag