How to use date-fns

Comprehensive date-fns code examples:

How to use date-fns.formatDistanceToNow:

8
9
10
11
12
13
14
15
16
17
    console.log(err);
}
else {
    let user = await server.players[server.playersMap.get(message.author.id)];
    if (user && user !== undefined){
        const remainingTime = formatDistanceToNow(server.apResetTime, { addSuffix: false }, { includeSeconds: true })

        const embed = new MessageEmbed()
            .setColor('#800b03')
            .setDescription(`**${message.author.username}'s AP: ${user.currentAp}/${user.maxAp}**\nYou will recover 5AP in **${remainingTime}**`)

How to use date-fns.formatRelative:

153
154
155
156
157
158
159

// Relative dates
const fnsDate30Days = add(fnsDate, { days: 30 });

console.log('+30 days', format(fnsDate30Days, 'LLL d, yyyy'));
console.log('relative', formatRelative(fnsDate30Days, fnsDate30Days));
console.log('duration', formatDistance(fnsDate30Days, fnsDate));

How to use date-fns.eachQuarterOfInterval:

135
136
137
138
139
140
141
142
143
    ...prev,
    { name: cur.name, dates: [`${formatDate(START_DATE)}..${formatDate(END_DATE)}`] },
  ];
}

const interval = eachQuarterOfInterval({
  start: START_DATE,
  end: END_DATE,
});

How to use date-fns.endOfDecade:

176
177
178
179
180
181
182
183
184
185
        required: false,
    },
},
setup(props, { emit }) {
    const from = vue.computed(() => dateFns.startOfDecade(props.pageDate));
    const to = vue.computed(() => dateFns.endOfDecade(props.pageDate));
    const isEnabled = (target, lower, upper) => {
        if (!lower && !upper)
            return true;
        if (lower && dateFns.getYear(target) < dateFns.getYear(lower))

How to use date-fns.startOfDecade:

175
176
177
178
179
180
181
182
183
184
        type: Date,
        required: false,
    },
},
setup(props, { emit }) {
    const from = vue.computed(() => dateFns.startOfDecade(props.pageDate));
    const to = vue.computed(() => dateFns.endOfDecade(props.pageDate));
    const isEnabled = (target, lower, upper) => {
        if (!lower && !upper)
            return true;

How to use date-fns.formatDistanceToNowStrict:

175
176
177
178
179
180
181
182
183
184

  return result
}

function formatDistanceAbbr (date) {
  return formatDistanceToNowStrict(date, {
    locale: {
      ...locale,
      formatDistance
    }

How to use date-fns.getWeek:

75
76
77
78
79
80
81
82
83
84

const days = [];

for (let i = 0; i <= totalDays; i += 1) {
  const date = datefns.addDays(startOfYear, i);
  const week = datefns.getWeek(date, { locale });
  const weekday = datefns.getDay(date);
  const day = datefns.format(date, 'd', { locale });

  days.push({

How to use date-fns.eachWeekOfInterval:

71
72
73
74
75
76
77
78
79
80
81
  return eachDayOfInterval({start: from, end: to})
    .map(start => [formatDate(start), formatDate(start)])
}


function getWeeks(from = BEGINNING, to = new Date()) {
  return eachWeekOfInterval({start: from, end: to}, {weekStartsOn: 1})
    .map(start => {
      const end = endOfWeek(start, {weekStartsOn: 1})
      return [formatDate(start), formatDate(end)]
    })

How to use date-fns.distanceInWordsToNow:

54
55
56
57
58
59
60
61
62
63
exports.differenceInSecondsImpl = dateFns.differenceInSeconds
exports.differenceInWeeksImpl = dateFns.differenceInWeeks
exports.differenceInYearsImpl = dateFns.differenceInYears
exports.distanceInWordsImpl = dateFns.distanceInWords
exports.distanceInWordsStrictImpl = dateFns.distanceInWordsStrict
exports.distanceInWordsToNowImpl = dateFns.distanceInWordsToNow
exports.eachDayImpl = dateFns.eachDay
exports.endOfDayImpl = dateFns.endOfDay
exports.endOfHourImpl = dateFns.endOfHour
exports.endOfISOWeekImpl = dateFns.endOfISOWeek

How to use date-fns.differenceInWeeks:

50
51
52
53
54
55
56
57
58
59
exports.differenceInMillisecondsImpl = dateFns.differenceInMilliseconds
exports.differenceInMinutesImpl = dateFns.differenceInMinutes
exports.differenceInMonthsImpl = dateFns.differenceInMonths
exports.differenceInQuartersImpl = dateFns.differenceInQuarters
exports.differenceInSecondsImpl = dateFns.differenceInSeconds
exports.differenceInWeeksImpl = dateFns.differenceInWeeks
exports.differenceInYearsImpl = dateFns.differenceInYears
exports.distanceInWordsImpl = dateFns.distanceInWords
exports.distanceInWordsStrictImpl = dateFns.distanceInWordsStrict
exports.distanceInWordsToNowImpl = dateFns.distanceInWordsToNow

How to use date-fns.endOfQuarter:

62
63
64
65
66
67
68
69
70
71
exports.endOfHourImpl = dateFns.endOfHour
exports.endOfISOWeekImpl = dateFns.endOfISOWeek
exports.endOfISOYearImpl = dateFns.endOfISOYear
exports.endOfMinuteImpl = dateFns.endOfMinute
exports.endOfMonthImpl = dateFns.endOfMonth
exports.endOfQuarterImpl = dateFns.endOfQuarter
exports.endOfSecondImpl = dateFns.endOfSecond
exports.endOfTodayImpl = dateFns.endOfToday
exports.endOfTomorrowImpl = dateFns.endOfTomorrow
exports.endOfWeekImpl = dateFns.endOfWeek

How to use date-fns.differenceInCalendarWeeks:

40
41
42
43
44
45
46
47
48
49
exports.differenceInCalendarDaysImpl = dateFns.differenceInCalendarDays
exports.differenceInCalendarISOWeeksImpl = dateFns.differenceInCalendarISOWeeks
exports.differenceInCalendarISOYearsImpl = dateFns.differenceInCalendarISOYears
exports.differenceInCalendarMonthsImpl = dateFns.differenceInCalendarMonths
exports.differenceInCalendarQuartersImpl = dateFns.differenceInCalendarQuarters
exports.differenceInCalendarWeeksImpl = dateFns.differenceInCalendarWeeks
exports.differenceInCalendarYearsImpl = dateFns.differenceInCalendarYears
exports.differenceInDaysImpl = dateFns.differenceInDays
exports.differenceInHoursImpl = dateFns.differenceInHours
exports.differenceInISOYearsImpl = dateFns.differenceInISOYears

How to use date-fns.differenceInCalendarQuarters:

39
40
41
42
43
44
45
46
47
48
exports.compareDescImpl = dateFns.compareDesc
exports.differenceInCalendarDaysImpl = dateFns.differenceInCalendarDays
exports.differenceInCalendarISOWeeksImpl = dateFns.differenceInCalendarISOWeeks
exports.differenceInCalendarISOYearsImpl = dateFns.differenceInCalendarISOYears
exports.differenceInCalendarMonthsImpl = dateFns.differenceInCalendarMonths
exports.differenceInCalendarQuartersImpl = dateFns.differenceInCalendarQuarters
exports.differenceInCalendarWeeksImpl = dateFns.differenceInCalendarWeeks
exports.differenceInCalendarYearsImpl = dateFns.differenceInCalendarYears
exports.differenceInDaysImpl = dateFns.differenceInDays
exports.differenceInHoursImpl = dateFns.differenceInHours

How to use date-fns.differenceInCalendarISOYears:

37
38
39
40
41
42
43
44
45
46
exports.closestToImpl = dateFns.closestTo
exports.compareAscImpl = dateFns.compareAsc
exports.compareDescImpl = dateFns.compareDesc
exports.differenceInCalendarDaysImpl = dateFns.differenceInCalendarDays
exports.differenceInCalendarISOWeeksImpl = dateFns.differenceInCalendarISOWeeks
exports.differenceInCalendarISOYearsImpl = dateFns.differenceInCalendarISOYears
exports.differenceInCalendarMonthsImpl = dateFns.differenceInCalendarMonths
exports.differenceInCalendarQuartersImpl = dateFns.differenceInCalendarQuarters
exports.differenceInCalendarWeeksImpl = dateFns.differenceInCalendarWeeks
exports.differenceInCalendarYearsImpl = dateFns.differenceInCalendarYears

How to use date-fns.subWeeks:

176
177
178
179
180
181
182
exports.subMillisecondsImpl = dateFns.subMilliseconds
exports.subMinutesImpl = dateFns.subMinutes
exports.subMonthsImpl = dateFns.subMonths
exports.subQuartersImpl = dateFns.subQuarters
exports.subSecondsImpl = dateFns.subSeconds
exports.subWeeksImpl = dateFns.subWeeks
exports.subYearsImpl = dateFns.subYears

How to use date-fns.differenceInISOYears:

44
45
46
47
48
49
50
51
52
53
exports.differenceInCalendarQuartersImpl = dateFns.differenceInCalendarQuarters
exports.differenceInCalendarWeeksImpl = dateFns.differenceInCalendarWeeks
exports.differenceInCalendarYearsImpl = dateFns.differenceInCalendarYears
exports.differenceInDaysImpl = dateFns.differenceInDays
exports.differenceInHoursImpl = dateFns.differenceInHours
exports.differenceInISOYearsImpl = dateFns.differenceInISOYears
exports.differenceInMillisecondsImpl = dateFns.differenceInMilliseconds
exports.differenceInMinutesImpl = dateFns.differenceInMinutes
exports.differenceInMonthsImpl = dateFns.differenceInMonths
exports.differenceInQuartersImpl = dateFns.differenceInQuarters

How to use date-fns.endOfISOYear:

59
60
61
62
63
64
65
66
67
68
exports.distanceInWordsToNowImpl = dateFns.distanceInWordsToNow
exports.eachDayImpl = dateFns.eachDay
exports.endOfDayImpl = dateFns.endOfDay
exports.endOfHourImpl = dateFns.endOfHour
exports.endOfISOWeekImpl = dateFns.endOfISOWeek
exports.endOfISOYearImpl = dateFns.endOfISOYear
exports.endOfMinuteImpl = dateFns.endOfMinute
exports.endOfMonthImpl = dateFns.endOfMonth
exports.endOfQuarterImpl = dateFns.endOfQuarter
exports.endOfSecondImpl = dateFns.endOfSecond

How to use date-fns.subISOYears:

170
171
172
173
174
175
176
177
178
179
exports.startOfWeekImpl = dateFns.startOfWeek
exports.startOfYearImpl = dateFns.startOfYear
exports.startOfYesterdayImpl = dateFns.startOfYesterday
exports.subDaysImpl = dateFns.subDays
exports.subHoursImpl = dateFns.subHours
exports.subISOYearsImpl = dateFns.subISOYears
exports.subMillisecondsImpl = dateFns.subMilliseconds
exports.subMinutesImpl = dateFns.subMinutes
exports.subMonthsImpl = dateFns.subMonths
exports.subQuartersImpl = dateFns.subQuarters

How to use date-fns.getMinutes:

81
82
83
84
85
86
87
88
89
90
exports.getISODayImpl = dateFns.getISODay
exports.getISOWeekImpl = dateFns.getISOWeek
exports.getISOWeeksInYearImpl = dateFns.getISOWeeksInYear
exports.getISOYearImpl = dateFns.getISOYear
exports.getMillisecondsImpl = dateFns.getMilliseconds
exports.getMinutesImpl = dateFns.getMinutes
exports.getMonthImpl = dateFns.getMonth
exports.getOverlappingDaysInRangesImpl = dateFns.getOverlappingDaysInRanges
exports.getQuarterImpl = dateFns.getQuarter
exports.getSecondsImpl = dateFns.getSeconds

How to use date-fns.getISOWeek:

77
78
79
80
81
82
83
84
85
86
exports.getDayOfYearImpl = dateFns.getDayOfYear
exports.getDaysInMonthImpl = dateFns.getDaysInMonth
exports.getDaysInYearImpl = dateFns.getDaysInYear
exports.getHoursImpl = dateFns.getHours
exports.getISODayImpl = dateFns.getISODay
exports.getISOWeekImpl = dateFns.getISOWeek
exports.getISOWeeksInYearImpl = dateFns.getISOWeeksInYear
exports.getISOYearImpl = dateFns.getISOYear
exports.getMillisecondsImpl = dateFns.getMilliseconds
exports.getMinutesImpl = dateFns.getMinutes

How to use date-fns.getISODay:

76
77
78
79
80
81
82
83
84
85
exports.getDayImpl = dateFns.getDay
exports.getDayOfYearImpl = dateFns.getDayOfYear
exports.getDaysInMonthImpl = dateFns.getDaysInMonth
exports.getDaysInYearImpl = dateFns.getDaysInYear
exports.getHoursImpl = dateFns.getHours
exports.getISODayImpl = dateFns.getISODay
exports.getISOWeekImpl = dateFns.getISOWeek
exports.getISOWeeksInYearImpl = dateFns.getISOWeeksInYear
exports.getISOYearImpl = dateFns.getISOYear
exports.getMillisecondsImpl = dateFns.getMilliseconds

How to use date-fns.getDayOfYear:

72
73
74
75
76
77
78
79
80
81
exports.endOfYearImpl = dateFns.endOfYear
exports.endOfYesterdayImpl = dateFns.endOfYesterday
exports.formatImpl = dateFns.format
exports.getDateImpl = dateFns.getDate
exports.getDayImpl = dateFns.getDay
exports.getDayOfYearImpl = dateFns.getDayOfYear
exports.getDaysInMonthImpl = dateFns.getDaysInMonth
exports.getDaysInYearImpl = dateFns.getDaysInYear
exports.getHoursImpl = dateFns.getHours
exports.getISODayImpl = dateFns.getISODay

How to use date-fns.getOverlappingDaysInRanges:

83
84
85
86
87
88
89
90
91
92
exports.getISOWeeksInYearImpl = dateFns.getISOWeeksInYear
exports.getISOYearImpl = dateFns.getISOYear
exports.getMillisecondsImpl = dateFns.getMilliseconds
exports.getMinutesImpl = dateFns.getMinutes
exports.getMonthImpl = dateFns.getMonth
exports.getOverlappingDaysInRangesImpl = dateFns.getOverlappingDaysInRanges
exports.getQuarterImpl = dateFns.getQuarter
exports.getSecondsImpl = dateFns.getSeconds
exports.getTimeImpl = dateFns.getTime
exports.getYearImpl = dateFns.getYear

How to use date-fns.lastDayOfISOWeek:

129
130
131
132
133
134
135
136
137
138
exports.isValidImpl = dateFns.isValid
exports.isWednesdayImpl = dateFns.isWednesday
exports.isWeekendImpl = dateFns.isWeekend
exports.isWithinRangeImpl = dateFns.isWithinRange
exports.isYesterdayImpl = dateFns.isYesterday
exports.lastDayOfISOWeekImpl = dateFns.lastDayOfISOWeek
exports.lastDayOfISOYearImpl = dateFns.lastDayOfISOYear
exports.lastDayOfMonthImpl = dateFns.lastDayOfMonth
exports.lastDayOfQuarterImpl = dateFns.lastDayOfQuarter
exports.lastDayOfWeekImpl = dateFns.lastDayOfWeek

How to use date-fns.getISOYear:

79
80
81
82
83
84
85
86
87
88
exports.getDaysInYearImpl = dateFns.getDaysInYear
exports.getHoursImpl = dateFns.getHours
exports.getISODayImpl = dateFns.getISODay
exports.getISOWeekImpl = dateFns.getISOWeek
exports.getISOWeeksInYearImpl = dateFns.getISOWeeksInYear
exports.getISOYearImpl = dateFns.getISOYear
exports.getMillisecondsImpl = dateFns.getMilliseconds
exports.getMinutesImpl = dateFns.getMinutes
exports.getMonthImpl = dateFns.getMonth
exports.getOverlappingDaysInRangesImpl = dateFns.getOverlappingDaysInRanges

How to use date-fns.areRangesOverlapping:

30
31
32
33
34
35
36
37
38
39
exports.addMonthsImpl = dateFns.addMonths
exports.addQuartersImpl = dateFns.addQuarters
exports.addSecondsImpl = dateFns.addSeconds
exports.addWeeksImpl = dateFns.addWeeks
exports.addYearsImpl = dateFns.addYears
exports.areRangesOverlappingImpl = dateFns.areRangesOverlapping
exports.closestIndexToImpl = dateFns.closestIndexTo
exports.closestToImpl = dateFns.closestTo
exports.compareAscImpl = dateFns.compareAsc
exports.compareDescImpl = dateFns.compareDesc

How to use date-fns.getDaysInYear:

74
75
76
77
78
79
80
81
82
83
exports.formatImpl = dateFns.format
exports.getDateImpl = dateFns.getDate
exports.getDayImpl = dateFns.getDay
exports.getDayOfYearImpl = dateFns.getDayOfYear
exports.getDaysInMonthImpl = dateFns.getDaysInMonth
exports.getDaysInYearImpl = dateFns.getDaysInYear
exports.getHoursImpl = dateFns.getHours
exports.getISODayImpl = dateFns.getISODay
exports.getISOWeekImpl = dateFns.getISOWeek
exports.getISOWeeksInYearImpl = dateFns.getISOWeeksInYear

How to use date-fns.lastDayOfYear:

134
135
136
137
138
139
140
141
142
143
exports.lastDayOfISOWeekImpl = dateFns.lastDayOfISOWeek
exports.lastDayOfISOYearImpl = dateFns.lastDayOfISOYear
exports.lastDayOfMonthImpl = dateFns.lastDayOfMonth
exports.lastDayOfQuarterImpl = dateFns.lastDayOfQuarter
exports.lastDayOfWeekImpl = dateFns.lastDayOfWeek
exports.lastDayOfYearImpl = dateFns.lastDayOfYear
exports.maxImpl = function(dates) {
  return dateFns.max.apply(null, dates)
}
exports.minImpl = function(dates) {

How to use date-fns.lastDayOfWeek:

133
134
135
136
137
138
139
140
141
142
exports.isYesterdayImpl = dateFns.isYesterday
exports.lastDayOfISOWeekImpl = dateFns.lastDayOfISOWeek
exports.lastDayOfISOYearImpl = dateFns.lastDayOfISOYear
exports.lastDayOfMonthImpl = dateFns.lastDayOfMonth
exports.lastDayOfQuarterImpl = dateFns.lastDayOfQuarter
exports.lastDayOfWeekImpl = dateFns.lastDayOfWeek
exports.lastDayOfYearImpl = dateFns.lastDayOfYear
exports.maxImpl = function(dates) {
  return dateFns.max.apply(null, dates)
}

How to use date-fns.lastDayOfQuarter:

132
133
134
135
136
137
138
139
140
141
exports.isWithinRangeImpl = dateFns.isWithinRange
exports.isYesterdayImpl = dateFns.isYesterday
exports.lastDayOfISOWeekImpl = dateFns.lastDayOfISOWeek
exports.lastDayOfISOYearImpl = dateFns.lastDayOfISOYear
exports.lastDayOfMonthImpl = dateFns.lastDayOfMonth
exports.lastDayOfQuarterImpl = dateFns.lastDayOfQuarter
exports.lastDayOfWeekImpl = dateFns.lastDayOfWeek
exports.lastDayOfYearImpl = dateFns.lastDayOfYear
exports.maxImpl = function(dates) {
  return dateFns.max.apply(null, dates)

date-fns functions:

Sorted by popularity