How to use the isBefore function from date-fns

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

date-fns.isBefore is a function that checks whether a date is before another date.

21
22
23
24
25
26
27
28
29
30
});

eleventyConfig.addCollection('pastEvents', (collectionApi) => {
        return collectionApi
                .getFilteredByGlob('./src/schedule/*.md')
                .filter((event) => isValidEvent(event) && isBefore(new Date(event.data.date), new Date()));
});

eleventyConfig.addCollection('upcomingEvents', (collectionApi) => {
        const serverEvents = collectionApi
fork icon11
star icon14
watch icon5

16
17
18
19
20
21
22
23
24
25

module.exports = {
        eleventyComputed: {
                calendarLinks: {
                        isPastEvent: ({ date: start }) => {
                                return isBefore(new Date(), new Date(start));
                        },
                        googleLink: ({ title, description, date: start }) => {
                                return google({
                                        title,
fork icon11
star icon14
watch icon5

How does date-fns.isBefore work?

date-fns.isBefore is a function that checks if the first date is before the second date based on their values, taking into account their timezones, and returns a boolean value. It first normalizes the input dates by converting them into Date objects. Then, it compares the timestamps of the two dates and returns true if the first date is before the second date. If the timestamps are equal, it checks if the first date is in an earlier timezone than the second date and returns true if it is. If the timestamps and timezones are equal, it returns false.

212
213
214
215
216
217
218
219
220
221
// Add amount to YTD number
yearData.ytd += payment.amount;

// Check if payment is same month and <= current date
if(dateFns.isSameMonth(paymentDate, currDate) &&
    (dateFns.isSameDay(paymentDate, currDate) || dateFns.isBefore(paymentDate, currDate))) {

  // Add amount to MTD number
  yearData.mtd += payment.amount;
}
fork icon2
star icon0
watch icon5

14
15
16
17
18
19
20
21
22
 * dateIsPast
 */

function dateIsPast(date) {
  const parsed = parseISO(date);
  return isBefore(parsed, new Date().getTime());
}

module.exports.dateIsPast = dateIsPast;
fork icon0
star icon3
watch icon2

Ai Example

1
2
3
4
5
6
7
8
9
10
const { isBefore } = require("date-fns");

const date1 = new Date(2022, 0, 1); // January 1, 2022
const date2 = new Date(2023, 0, 1); // January 1, 2023

if (isBefore(date1, date2)) {
  console.log("date1 is before date2");
} else {
  console.log("date1 is after date2");
}

In this example, isBefore is used to compare two dates and determine if date1 is before date2. If it is, the function logs 'date1 is before date2' to the console.

926
927
928
929
930
931
932
933
934
935
936
937
const getDateObj = (dateString) => parse(dateString, 'yyyy-MM-dd', new Date());
const formatDate = (dateObj) => format(dateObj, 'yyyy-MM-dd');


const isAFutureDate = (dateString) => {
  const date = getDateObj(dateString);
  return isBefore(getTodayObj(), date);
};


const isToday = (dateString) => {
  const date = getDateObj(dateString);
fork icon0
star icon2
watch icon1

+ 64 other calls in file

399
400
401
402
403
404
405
406
407
408
  if (isBefore(now, voteStart)) {
    logger.error('Requesting contest before voting window opened');
    res.status(404).send('Contest voting window is not open yet');
    return;
  }
  response.isContestMode = isBefore(now, voteEnd);
  response.voteEnd = voteEnd;
}

if (response.isContestMode && !winnersThreadId) {
fork icon0
star icon2
watch icon1

+ 24 other calls in file

89
90
91
92
93
94
95
96
97
98
exports.getQuarterImpl = dateFns.getQuarter
exports.getSecondsImpl = dateFns.getSeconds
exports.getTimeImpl = dateFns.getTime
exports.getYearImpl = dateFns.getYear
exports.isAfterImpl = dateFns.isAfter
exports.isBeforeImpl = dateFns.isBefore
exports.isDateImpl = dateFns.isDate
exports.isEqualImpl = dateFns.isEqual
exports.isFirstDayOfMonthImpl = dateFns.isFirstDayOfMonth
exports.isFridayImpl = dateFns.isFriday
fork icon0
star icon2
watch icon1

+ 16 other calls in file

36
37
38
39
40
41
42
43
44
45

eleventyConfig.addCollection('pastEvents', collection => {
  return collection
    .getFilteredByTag('event')
    //.getFilteredByGlob('./src/veranstaltungen/*.md')
    .filter((veranstaltung) => isBefore(new Date(veranstaltung.data.date), new Date()));
});

// human readable date
eleventyConfig.addFilter("readableDate", (dateObj) => {
fork icon88
star icon0
watch icon1

838
839
840
841
842
843
844
845
846
847
848
849
const getToday = () => new Date();
const parseDate = (dateString) => parse(dateString, 'yyyy-MM-dd', new Date());


const isAFutureDate = (dateString) => {
  const date = parseDate(dateString);
  return isBefore(getToday(), date);
};


const isToday = (dateString) => {
  const date = parseDate(dateString);
fork icon0
star icon2
watch icon1

+ 11 other calls in file

193
194
195
196
197
198
199
200
201
202
    unpaid?.invoice?.paidCode !== 'P' && !unpaid?.isApproved,
);

const dueDate =
  vessel && shipper && getVesselControlDueDate(vessel, shipper);
const isPast = dueDate && isBefore(dueDate, startOfWeek);
const isCurrent =
  dueDate &&
  isDateGreaterThanOrEqualTo(dueDate, startOfWeek) &&
  isDateLessThanOrEqualTo(dueDate, endOfWeek);
fork icon0
star icon0
watch icon1

35
36
37
38
39
40
41
42
43
44
if (req.query.to) {
  result = result.filter(item => {
    if (item.date === req.query.to) {
      return true;
    }
    return isBefore(transformedDate(item.date), transformedDate(req.query.to));
  });
}

return res.json({ status: "OK", data: result });
fork icon0
star icon0
watch icon1

+ 24 other calls in file

62
63
64
65
66
67
68
69
70
71
72
73
const data1 = new Date(2023, 0, 5, 12, 45);
const data2 = new Date(2023, 0, 8, 12, 35);
const metIsAfter = isAfter(data2, data1);
console.log(metIsAfter);


const metIsBefore = isBefore(data2, data1);
console.log(metIsBefore);


// Metodos startOf- zera depois do incremento
//metodos endOf-todos os que vem depois fica no limite
fork icon0
star icon0
watch icon0

+ 4 other calls in file

203
204
205
206
207
208
209
210
211
212
    const end = dateFns.getYear(to.value);
    return `${start} - ${end}`;
});
const leftDisabled = vue.computed(() => props.lowerLimit &&
    (dateFns.getDecade(props.lowerLimit) === dateFns.getDecade(props.pageDate) ||
        dateFns.isBefore(props.pageDate, props.lowerLimit)));
const rightDisabled = vue.computed(() => props.upperLimit &&
    (dateFns.getDecade(props.upperLimit) === dateFns.getDecade(props.pageDate) ||
        dateFns.isAfter(props.pageDate, props.upperLimit)));
const previousPage = () => emit('update:pageDate', dateFns.subYears(props.pageDate, 10));
fork icon0
star icon0
watch icon0

+ 49 other calls in file

74
75
76
77
78
79
80
81
82
83
84
    new Date(1987, 1, 11));
console.log("isAfter:", depois);


//Método isBefore
//Nesse exemplo verifica se uma data é antes da outra
const antes = isBefore(
    new Date(1989, 6, 10),
    new Date(1987, 1, 11));
console.log("isBefore:", antes);

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)