Skip to content

Commit 6d96322

Browse files
committed
fix: accept date-like objects in formatDate
1 parent 862b438 commit 6d96322

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/common/is-date.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const objectToString = {}.toString;
2-
const DATE_STRING = "[object Date]";
1+
function isFunction(fun) {
2+
return typeof(fun) === 'function';
3+
}
34

45
export default function isDate(value) {
5-
return objectToString.call(value) === DATE_STRING;
6-
}
6+
return Boolean(value) && isFunction(value.getTime) && isFunction(value.getMonth);
7+
}

src/dates.d.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,63 @@ export interface DateFormatOptions {
8181
timeZoneName?: 'short' | 'long';
8282
}
8383

84+
/** A Date-like object that provides read-only access to the date parts. */
85+
export interface ImmutableDate {
86+
/** Gets the time value in milliseconds. */
87+
getTime(): number;
88+
89+
/** Gets the year, using local time. */
90+
getFullYear(): number;
91+
92+
/** Gets the year using Universal Coordinated Time (UTC). */
93+
getUTCFullYear(): number;
94+
95+
/** Gets the month, using local time. */
96+
getMonth(): number;
97+
98+
/** Gets the month of a Date object using Universal Coordinated Time (UTC). */
99+
getUTCMonth(): number;
100+
101+
/** Gets the day-of-the-month, using local time. */
102+
getDate(): number;
103+
104+
/** Gets the day-of-the-month, using Universal Coordinated Time (UTC). */
105+
getUTCDate(): number;
106+
107+
/** Gets the day of the week, using local time. */
108+
getDay(): number;
109+
110+
/** Gets the day of the week using Universal Coordinated Time (UTC). */
111+
getUTCDay(): number;
112+
113+
/** Gets the hours in a date, using local time. */
114+
getHours(): number;
115+
116+
/** Gets the hours value in a Date object using Universal Coordinated Time (UTC). */
117+
getUTCHours(): number;
118+
119+
/** Gets the minutes of a Date object, using local time. */
120+
getMinutes(): number;
121+
122+
/** Gets the minutes of a Date object using Universal Coordinated Time (UTC). */
123+
getUTCMinutes(): number;
124+
125+
/** Gets the seconds of a Date object, using local time. */
126+
getSeconds(): number;
127+
128+
/** Gets the seconds of a Date object using Universal Coordinated Time (UTC). */
129+
getUTCSeconds(): number;
130+
131+
/** Gets the milliseconds of a Date, using local time. */
132+
getMilliseconds(): number;
133+
134+
/** Gets the milliseconds of a Date object using Universal Coordinated Time (UTC). */
135+
getUTCMilliseconds(): number;
136+
137+
/** Gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC). */
138+
getTimezoneOffset(): number;
139+
}
140+
84141
/**
85142
* Converts a `Date` object to a string based on the specified format and locale.
86143
*
@@ -89,7 +146,7 @@ export interface DateFormatOptions {
89146
* @param locale - The optional locale `id`. If not specified, the `id` of the `"en"` locale is used.
90147
* @returns - The formatted date.
91148
*/
92-
export function formatDate(value: Date, format: string|DateFormatOptions, locale?: string): string;
149+
export function formatDate(value: Date | ImmutableDate, format: string|DateFormatOptions, locale?: string): string;
93150

94151
/**
95152
* Converts a string to a `Date` object based on the specified format and locale.

0 commit comments

Comments
 (0)