How to use the formatDistanceToNow function from date-fns

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

date-fns.formatDistanceToNow is a function that returns a human-readable string representing the distance between the given date and the current time.

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}**`)
fork icon1
star icon0
watch icon1

154
155
156
157
158
159
160
161
162
163
    console.log(err);
}
else {
    if (server.hgwEndDate !== 0){

        const remainingTime = formatDistanceToNow(server.hgwEndDate, { addSuffix: false }, { includeSeconds: true })


        let players = await server.players
        if(players){
fork icon1
star icon0
watch icon1

How does date-fns.formatDistanceToNow work?

date-fns.formatDistanceToNow is a function that takes in a date as an argument and returns a string that represents the distance between that date and the current time, in words such as "5 minutes ago" or "in 3 hours". It also allows for additional configuration options to customize the output format.

201
202
203
204
205
206
207
208
209
210
    value: lastPrice.toPrecision(4) + ' ◎',
    inline: true,
  },
  {
    name: 'Last Sale',
    value: formatDistanceToNow(lastTx),
    inline: true,
  },
)
.setTimestamp()
fork icon0
star icon0
watch icon0

8
9
10
11
12
13
14
15
16
17
    // 1분 미만일땐 방금 전 표기
    return '방금 전';
  }
  if (diff < 60 * 60 * 24 * 3) {
    // 3일 미만일땐 시간차이 출력(몇시간 전, 몇일 전)
    return formatDistanceToNow(d, {addSuffix: true, locale: ko});
  }
  return format(d, 'PPP EEE p', {locale: ko}); // 날짜 포맷
};

fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
import { formatDistanceToNow } from "date-fns";

const distance = formatDistanceToNow(new Date(2022, 0, 1), { addSuffix: true });
console.log(distance); // prints "in about 1 year"

This example will format the distance between the current date and January 1st, 2022 as a human-readable string, using the addSuffix option to include a string like "in" or "ago". The resulting string will be logged to the console.

18
19
20
21
22
23
24
25
26
27
    // 1분 미만일땐 방금 전 표기
    return "방금 전";
  }
  if (diff < 60 * 60 * 24 * 3) {
    // 3일 미만일땐 시간차이 출력(몇시간 전, 몇일 전)
    return formatDistanceToNow(d, { addSuffix: true, locale: ko });
  }
  return format(d, "PPP EEE p", { locale: ko }); // 날짜 포맷
};

fork icon0
star icon0
watch icon0

53
54
55
56
57
58
59
60
61
		incidents.push({
			name: incident.name,
			status: incident.status,
			impact: incident.impact,
			componentId: incident.components[0].id,
			started: formatDistanceToNow(startedAt, {})
		});
	}
});
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)