How to use the isWithinInterval function from date-fns

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

date-fns.isWithinInterval is a function that checks whether a given date falls within a specified time interval.

54
55
56
57
58
59
60
61
62
63

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 }));
const thisMonth = rows.filter(row => dateFns.isWithinInterval(dateFns.parseISO(row.date), { start: startOfBootMonth, end: endOfBootMonth }));
const thisYear = rows.filter(row => dateFns.isWithinInterval(dateFns.parseISO(row.date), { start: startOfBootYear, end: endOfBootYear }));

daily = rows.find(row => row.date === dateFns.format(new Date(), 'yyyy-MM-dd')).count;
fork icon9
star icon40
watch icon0

+ 13 other calls in file

45
46
47
48
49
50
51
52
53
54
}

isInRange(val) {
  const { range } = this.props;
  if (Array.isArray(range) && range[0] && range[1]) {
    return isWithinInterval(val, { start: range[0], end: range[1] });
  }

  return false;
}
fork icon6
star icon0
watch icon3

+ 3 other calls in file

How does date-fns.isWithinInterval work?

date-fns.isWithinInterval works by comparing a given date to a specified time interval, which is defined by a start date and an end date.

To use date-fns.isWithinInterval, you pass it two arguments: the date you want to check, and an object that represents the time interval you want to check against. The interval object should have two properties: start, which represents the start date of the interval, and end, which represents the end date of the interval.

When date-fns.isWithinInterval is called, it checks whether the given date falls within the specified time interval. If the date falls within the interval (i.e., if it is greater than or equal to the start date and less than or equal to the end date), then date-fns.isWithinInterval returns true. Otherwise, it returns false.

It's worth noting that date-fns.isWithinInterval uses the local time zone of the computer running the code to determine the time zone of the start and end dates in the interval object. If you need to work with dates in a specific time zone, you can use other functions provided by the date-fns library to convert the dates to the desired time zone before passing them to date-fns.isWithinInterval.

361
362
363
364
365
366
367
368
369
370
const resEnd = new Date(resEndTime);


if (
  isWithinInterval(resStart, { start: rStart, end: rEnd }) ||
  isWithinInterval(resEnd, { start: rStart, end: rEnd }) ||
  isWithinInterval(rStart, { start: resStart, end: resEnd }) ||
  isWithinInterval(rEnd, { start: resStart, end: resEnd })
) {
  return res.status(400).json({
fork icon1
star icon0
watch icon0

+ 3 other calls in file

137
138
139
140
141
142
143
144
145
146
},
fromUnix(val) {
  return fromUnixTime(val)
},
isBetween(t, start, end) {
  return isWithinInterval(t, {start: parseISO(start), end: parseISO(end)})
},
hour() {
  return 3600
},
fork icon0
star icon0
watch icon0

+ 47 other calls in file

Ai Example

1
2
3
4
5
6
7
const { isWithinInterval, parse } = require("date-fns");

const startDate = parse("2022-01-01");
const endDate = parse("2022-01-31");
const dateToCheck = parse("2022-01-15");

console.log(isWithinInterval(dateToCheck, { start: startDate, end: endDate })); // true

In this example, we first import isWithinInterval and parse from the date-fns library. We then create three date objects using parse: startDate, endDate, and dateToCheck. startDate and endDate represent the start and end dates of the time interval we want to check against, while dateToCheck represents the date we want to check. We then use isWithinInterval to check whether dateToCheck falls within the time interval defined by startDate and endDate. As expected, isWithinInterval(dateToCheck, { start: startDate, end: endDate }) returns true since dateToCheck falls within the time interval. By using date-fns.isWithinInterval, we can programmatically determine whether a given date falls within a specified time interval, allowing us to perform operations on dates with confidence.

Other functions in date-fns

Sorted by popularity

function icon

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