Skip to content

Commit 4c6f1c0

Browse files
committed
relativeDateFormat: Accept ISO string for toDate and fromDate aswell
1 parent 12c337a commit 4c6f1c0

9 files changed

+43
-30
lines changed

cjs.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ declare const _exports: {
77
uniqueValues(array: any[], key: string, keys: any[]): any[];
88
relativeDateFormat(options: {
99
/**
10-
* Date object to format relative date from
10+
* Date object or date ISO string to format relative date from
1111
*/
12-
toDate: Date;
12+
toDate: string | Date;
1313
/**
14-
* Date object to be starting point (Default = new Date())
14+
* Date object or date ISO string to be starting point (Default = new Date())
1515
*/
16-
fromDate?: Date;
16+
fromDate?: string | Date;
1717
/**
1818
* Locale to format date into (Default = 'en')
1919
*/

es.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ declare const _exports: {
66
uniqueValues(array: any[], key: string, keys: any[]): any[];
77
relativeDateFormat(options: {
88
/**
9-
* Date object to format relative date from
9+
* Date object or date ISO string to format relative date from
1010
*/
11-
toDate: Date;
11+
toDate: string | Date;
1212
/**
13-
* Date object to be starting point (Default = new Date())
13+
* Date object or date ISO string to be starting point (Default = new Date())
1414
*/
15-
fromDate?: Date;
15+
fromDate?: string | Date;
1616
/**
1717
* Locale to format date into (Default = 'en')
1818
*/

lib/utilities.d.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ declare class Utilities {
3939
/**
4040
* RelativeDateOptions
4141
* @typedef {Object} RelativeDateOptions
42-
* @property {Date} toDate Date object to format relative date from
43-
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
42+
* @property {Date|String} toDate Date object or date ISO string to format relative date from
43+
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
4444
* @property {String} [locale] Locale to format date into (Default = 'en')
4545
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
4646
*/
@@ -57,13 +57,13 @@ declare class Utilities {
5757
*/
5858
static relativeDateFormat(options: {
5959
/**
60-
* Date object to format relative date from
60+
* Date object or date ISO string to format relative date from
6161
*/
62-
toDate: Date;
62+
toDate: Date | string;
6363
/**
64-
* Date object to be starting point (Default = new Date())
64+
* Date object or date ISO string to be starting point (Default = new Date())
6565
*/
66-
fromDate?: Date;
66+
fromDate?: Date | string;
6767
/**
6868
* Locale to format date into (Default = 'en')
6969
*/

lib/utilities.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ module.exports = class Utilities {
130130
/**
131131
* RelativeDateOptions
132132
* @typedef {Object} RelativeDateOptions
133-
* @property {Date} toDate Date object to format relative date from
134-
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
133+
* @property {Date|String} toDate Date object or date ISO string to format relative date from
134+
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
135135
* @property {String} [locale] Locale to format date into (Default = 'en')
136136
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
137137
*/

lib/utilities_es.d.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ declare class Utilities {
3232
/**
3333
* RelativeDateOptions
3434
* @typedef {Object} RelativeDateOptions
35-
* @property {Date} toDate Date object to format relative date from
36-
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
35+
* @property {Date|String} toDate Date object or date ISO string to format relative date from
36+
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
3737
* @property {String} [locale] Locale to format date into (Default = 'en')
3838
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
3939
*/
@@ -50,13 +50,13 @@ declare class Utilities {
5050
*/
5151
static relativeDateFormat(options: {
5252
/**
53-
* Date object to format relative date from
53+
* Date object or date ISO string to format relative date from
5454
*/
55-
toDate: Date;
55+
toDate: Date | string;
5656
/**
57-
* Date object to be starting point (Default = new Date())
57+
* Date object or date ISO string to be starting point (Default = new Date())
5858
*/
59-
fromDate?: Date;
59+
fromDate?: Date | string;
6060
/**
6161
* Locale to format date into (Default = 'en')
6262
*/

lib/utilities_es.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ module.exports = class Utilities {
115115
/**
116116
* RelativeDateOptions
117117
* @typedef {Object} RelativeDateOptions
118-
* @property {Date} toDate Date object to format relative date from
119-
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
118+
* @property {Date|String} toDate Date object or date ISO string to format relative date from
119+
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
120120
* @property {String} [locale] Locale to format date into (Default = 'en')
121121
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
122122
*/

tests/util/relative-date-format.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ describe('formatRelativeDate', () => {
5252
})
5353
})
5454
})
55+
56+
test('"toDate" and "fromDate" passed as ISO strings', () => {
57+
const now = new Date()
58+
const futureDate = new Date(new Date().setDate(now.getDate() + 7))
59+
const formatted = formatRelativeDate({ toDate: now.toISOString(), fromDate: futureDate.toISOString() })
60+
expect(['in 7 days', 'in 1 week', '1 week ago'].includes(formatted)).toBe(true)
61+
})
5562
})

util/relative-date-format.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ export function formatRelativeDate({ toDate, fromDate, locale, options }: Relati
44
*/
55
export type RelativeDateOptions = {
66
/**
7-
* Date object to format relative date from
7+
* Date object or date ISO string to format relative date from
88
*/
9-
toDate: Date;
9+
toDate: Date | string;
1010
/**
11-
* Date object to be starting point (Default = new Date())
11+
* Date object or date ISO string to be starting point (Default = new Date())
1212
*/
13-
fromDate?: Date;
13+
fromDate?: Date | string;
1414
/**
1515
* Locale to format date into (Default = 'en')
1616
*/

util/relative-date-format.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const DIVISIONS = [
1111
/**
1212
* RelativeDateOptions
1313
* @typedef {Object} RelativeDateOptions
14-
* @property {Date} toDate Date object to format relative date from
15-
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
14+
* @property {Date|String} toDate Date object or date ISO string to format relative date from
15+
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
1616
* @property {String} [locale] Locale to format date into (Default = 'en')
1717
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
1818
*/
@@ -35,6 +35,12 @@ const getRelativeTimeFormatter = (locale, options) => {
3535
*/
3636
module.exports.formatRelativeDate = ({ toDate, fromDate = new Date(), locale = 'en', options = undefined }) => {
3737
if (toDate === null || toDate === undefined) throw new Error('Pass at least "toDate"')
38+
if (typeof toDate === 'string' && Date.parse(toDate) > 0) {
39+
toDate = new Date(toDate)
40+
}
41+
if (typeof fromDate === 'string' && Date.parse(fromDate) > 0) {
42+
fromDate = new Date(fromDate)
43+
}
3844

3945
const FORMATTER = getRelativeTimeFormatter(locale, options)
4046
let duration = (toDate - fromDate) / 1000

0 commit comments

Comments
 (0)