How to use the formatRelative function from date-fns

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

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));
fork icon0
star icon0
watch icon2

1
2
3
4
5
6
7
8
9
10

module.exports = function(eleventyConfig) {
  eleventyConfig.addPassthroughCopy('src/static');

  eleventyConfig.addNunjucksShortcode('timeAgo', function(ts) {
    return formatRelative(new Date(ts * 1000), new Date());
  });

  return {
    dir: {
fork icon0
star icon0
watch icon1

132
133
134
135
136
137
138
139
140
function formatDate(date) {
  return format(date, 'MM/dd/yyyy')
}

function formatDateTime(date) {
  return formatRelative(date, new Date())
}

const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/
fork icon0
star icon0
watch icon2

Other functions in date-fns

Sorted by popularity

function icon

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