-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateFormat.js
More file actions
97 lines (84 loc) · 3.59 KB
/
dateFormat.js
File metadata and controls
97 lines (84 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Wrapper functions for date-fns library
* @see https://date-fns.org/
* @see https://date-fns.org/v2.30.0/docs/format
* @see https://date-fns.org/v2.30.0/docs/parse
* @see https://date-fns.org/v2.30.0/docs/I18n
* @see https://date-fns.org/v2.30.0/docs/Unicode-Tokens
*/
import { format, formatDistance } from "date-fns";
const DEFAULT_FORMAT = "dd/MM/yyyy";
/**
* Helper function to format a date string.
* @param {string} dateString - The date string
* @param {string} formatString - The date format specified as [Unicode Technical Standard #35](https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table). Defaults to `dd/MM/yyyy`.
* @returns {string} - Formatted date string. Returns empty string if the `dateString` is empty. Returns the `dateString` if it is not a valid date string.
*/
export const formatDate = (dateString, formatString) => {
if (!dateString) return "";
const dateObj = new Date(dateString);
if (isNaN(dateObj)) {
return dateString;
}
// Fix basic format issues
formatString = (formatString || DEFAULT_FORMAT).replace(/ AA/, " a");
return format(dateObj, formatString);
};
/**
* Helper function to format a date-time string.
* Same as `formatDate` function but defaults to `dd/MM/yyyy hh:mm a`.
* @param {string} dateString - The date-time string
* @param {string} formatString - The date-time format specified as [Unicode Technical Standard #35](https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table). Defaults to `dd/MM/yyyy hh:mm a`.
* @returns {string} - Formatted date-time string. Returns empty string if the `dateString` is empty. Returns the `dateString` if it is not a valid date-time string.
*/
export const formatDateTime = (
dateString,
formatString = "dd/MM/yyyy hh:mm a"
) => formatDate(dateString, formatString);
/**
* Get the "when" string for the given Date object. Eg: "Today", "Yesterday", "Tomorrow".
* @param {Date} date - The Date object
* @returns {string} - The "when" string
* @example
* getWhenString(new Date()); // "Today"
* getWhenString(new Date(Date.now() - 24 * 60 * 60 * 1000)); // "Yesterday"
* getWhenString(new Date(Date.now() + 24 * 60 * 60 * 1000)); // "Tomorrow"
*/
export const getWhenString = (date) => {
if (!date) return "";
const today = new Date();
if (date.toDateString() === today.toDateString()) {
return "Today";
}
const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000);
if (date.toDateString() === yesterday.toDateString()) {
return "Yesterday";
}
const tomorrow = new Date(today.getTime() + 24 * 60 * 60 * 1000);
if (date.toDateString() === tomorrow.toDateString()) {
return "Tomorrow";
}
return "";
};
/**
* Return the distance between the given dates in words.
* @param {string} date - The date to compare
* @param {string} baseDate - The base date to compare against
* @param {object} [options] - Options to pass to formatter function
* @param {boolean} [options.addSuffix] - Whether to add suffix to the result like "ago" or "in"
* @param {boolean} [options.includeSeconds] - Whether to include seconds in the output
* @param {string} [options.locale] - Locale to use for formatting
* @returns {string} - The distance between the dates in words
* @example
* getDateDistance("2022-01-01", "2022-01-02"); // "1 day ago"
* getDateDistance("2022-01-02", "2022-01-01", { addSuffix: false }); // "in 1"
*/
export const getDateDistance = (date, baseDate, options = {}) => {
if (!date || !baseDate) return "";
const distance = formatDistance(new Date(date), new Date(baseDate), {
addSuffix: true,
includeSeconds: false,
...options,
});
return distance;
};