How to use the startOfWeek function from date-fns

Find comprehensive JavaScript date-fns.startOfWeek code examples handpicked from public code repositorys.

date-fns.startOfWeek is a function that returns the start of a week according to the given locale, and can also take options such as the first day of the week.

92
93
94
95
96
97
98
99
100
101
  return days;
}

getWeekdays() {
  const { locale } = this;
  const startOfWeek = datefns.startOfWeek(new Date(), { locale });
  const startIndex = datefns.getDay(startOfWeek);
  const weekdays = [];

  for (let i = 0; i < 7; i += 1) {
fork icon28
star icon313
watch icon7

+ 3 other calls in file

50
51
52
53
54
55
56
57
58
59
// Insert statistics entry for the boot day if it does not exist

db.all('SELECT * FROM statistics', [], (selectErr, rows) => {
	if (!rows) return Logger.warn('No statistics found.');

	const startOfBootWeek = dateFns.startOfWeek(new Date(), { weekStartsOn: 1 }), endOfBootWeek = dateFns.endOfWeek(new Date(), { weekStartsOn: 1 });
	const startOfBootMonth = dateFns.startOfMonth(new Date()), endOfBootMonth = dateFns.endOfMonth(new Date());
	const startOfBootYear = dateFns.startOfYear(new Date()), endOfBootYear = dateFns.endOfYear(new Date());

	const thisWeek = rows.filter(row => dateFns.isWithinInterval(dateFns.parseISO(row.date), { start: startOfBootWeek, end: endOfBootWeek }));
fork icon9
star icon40
watch icon0

How does date-fns.startOfWeek work?

date-fns.startOfWeek is a function that returns the start of a week for a given date based on the provided options, such as the starting day of the week. It takes a date object as input and returns a new date object representing the start of the week. By default, the week starts on Sunday, but this can be changed with the options parameter.

1035
1036
1037
1038
1039
1040
1041
1042
1043
const today = new Date();
const lastSunday = endOfWeek(today, { weekStartsOn: 0 });

const findDtr = await Dtr.find({
    projectId: projectId,
    createdAt: { $gte: startOfWeek(today, { weekStartsOn: 0 }), $lte: lastSunday }
}).exec();
const crewIds = findDtr.map(crewId => crewId.crewId)
const findCrew = await Crew.find({crewId: {$in: crewIds}}).exec()
fork icon0
star icon3
watch icon1

+ 7 other calls in file

165
166
167
168
169
170
171
172
173
174
exports.startOfMonthImpl = dateFns.startOfMonth
exports.startOfQuarterImpl = dateFns.startOfQuarter
exports.startOfSecondImpl = dateFns.startOfSecond
exports.startOfTodayImpl = dateFns.startOfToday
exports.startOfTomorrowImpl = dateFns.startOfTomorrow
exports.startOfWeekImpl = dateFns.startOfWeek
exports.startOfYearImpl = dateFns.startOfYear
exports.startOfYesterdayImpl = dateFns.startOfYesterday
exports.subDaysImpl = dateFns.subDays
exports.subHoursImpl = dateFns.subHours
fork icon0
star icon2
watch icon1

+ 16 other calls in file

Ai Example

1
2
3
4
5
6
const { startOfWeek } = require("date-fns");

const date = new Date("2022-04-12");
const startOfWeekDate = startOfWeek(date, { weekStartsOn: 1 });

console.log(startOfWeekDate); // Output: 2022-04-11T00:00:00.000Z

In this example, startOfWeek is used to find the start date of the week for the given date (in this case, April 12th, 2022). The weekStartsOn option is passed as the second argument to specify that the week starts on Monday (which is represented by 1 here). The output shows that the start date of the week is April 11th, 2022.

0
1
2
3
4
5
6
7
8
9
const { startOfWeek, addDays } = require('date-fns');

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

module.exports = (today = new Date()) => {
  const start = startOfWeek(today, { weekStartsOn: 6 });
  const satDisplay = `${months[start.getMonth()]} ${start.getDate()}`;
  const sun = addDays(start, 1);
  return `${satDisplay}/${sun.getDate()}`;
};
fork icon0
star icon1
watch icon2

32
33
34
35
36
37
38
39
40
41

class MenuSchedule extends PureComponent<any, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      weekStartDate:  dateFns.startOfWeek(new Date()),
      activeWeekRelativeToCurrentWeek: 0,
      isBusy: true
    };
  }
fork icon2
star icon0
watch icon3

117
118
119
120
121
122
123
124
125
126
    break;
case 'week':
    subIntervalBreaks = dateFns.eachWeekOfInterval(queryInterval);
    if (subIntervalBreaks.length === 0) {
        subIntervalBreaks = [
            dateFns.startOfWeek(queryInterval['start']),
            dateFns.addMilliseconds(dateFns.endOfWeek(queryInterval['end']), 1)
        ];
        break;
    }
fork icon1
star icon0
watch icon0

89
90
91
92
93
94
95
96
97
98
  case "tomorrow":
    return startOfTomorrow()
  case "yesterday":
    return startOfYesterday()
  case "week":
    return startOfWeek(val)
  case "month":
    return startOfMonth(val)
}
return val
fork icon0
star icon0
watch icon0

+ 47 other calls in file

418
419
420
421
422
423
424
425
426
427
const currentMonth = vue.computed(() => ({
    start: monthStart.value,
    end: monthEnd.value,
}));
const displayedInterval = vue.computed(() => ({
    start: dateFns.startOfWeek(monthStart.value, {
        weekStartsOn: props.weekStartsOn,
    }),
    end: dateFns.endOfWeek(monthEnd.value, {
        weekStartsOn: props.weekStartsOn,
fork icon0
star icon0
watch icon0

+ 9 other calls in file

467
468
469
470
471
472
473
474
475
const lastWeek = sub(today, { weeks: 1 });
const yesterday = sub(today, { days: 1 });

const thisMonthRange = { start: startOfMonth(today), end: endOfMonth(today) };
const lastMonthRange = { start: startOfMonth(lastMonth), end: endOfMonth(lastMonth) };
const thisWeekRange = { start: startOfWeek(today), end: endOfWeek(today) };
const lastWeekRange = { start: startOfWeek(lastWeek), end: endOfWeek(lastWeek) };
const todayRange = { start: startOfDay(today), end: endOfDay(today) };
const yesterdayRange = { start: startOfDay(yesterday), end: endOfDay(yesterday) };
fork icon0
star icon0
watch icon0

+ 3 other calls in file

25
26
27
28
29
30
31
32
33
34
    datePickerHeaderText.innerText = format(currentDate, "MMMM - yyyy")
    setupDates(selectedDate)
  }
//4. setup dates, startofweek gives 7 days, start of month gives whole month
function setupDates(selectedDate) {
    const firstWeekStart = startOfWeek(startOfMonth(currentDate))
    const lastWeekEnd = endOfWeek(endOfMonth(currentDate))
    const dates = eachDayOfInterval({ start: firstWeekStart, end: lastWeekEnd })
    dateGrid.innerHTML = ""
// Return the array of dates within the specified time interval.
fork icon0
star icon0
watch icon0

Other functions in date-fns

Sorted by popularity

function icon

date-fns.format is the most popular function in date-fns (3838 examples)