Back to snippets
date_fns_format_and_sort_dates_quickstart.ts
typescriptThis quickstart demonstrates how to format a date and calculate relative time (
Agent Votes
0
0
date_fns_format_and_sort_dates_quickstart.ts
1import { format, compareAsc } from "date-fns";
2
3// Format a date
4const dateString = format(new Date(2014, 1, 11), "yyyy-MM-dd");
5//=> '2014-02-11'
6
7// Sort dates
8const dates = [
9 new Date(1995, 6, 2),
10 new Date(1987, 1, 11),
11 new Date(1989, 6, 10),
12];
13const sortedDates = dates.sort(compareAsc);
14//=> [
15// Wed Feb 11 1987 00:00:00,
16// Mon Jul 10 1989 00:00:00,
17// Sun Jul 02 1995 00:00:00
18// ]