|
1 | | -function pad(value, width) { |
2 | | - var s = value + "", length = s.length; |
3 | | - return length < width ? new Array(width - length + 1).join(0) + s : s; |
4 | | -} |
5 | | - |
6 | | -function isUTCMidnight(date) { |
7 | | - return date.getUTCMilliseconds() === 0 |
8 | | - && date.getUTCSeconds() === 0 |
9 | | - && date.getUTCMinutes() === 0 |
10 | | - && date.getUTCHours() === 0; |
11 | | -} |
12 | | - |
13 | | -function formatYear(year) { |
14 | | - return year < 0 ? "-" + pad(-year, 6) |
15 | | - : year > 9999 ? "+" + pad(year, 6) |
16 | | - : pad(year, 4); |
17 | | -} |
| 1 | +import {format} from "isoformat"; |
18 | 2 |
|
19 | 3 | export default function formatDate(date) { |
20 | | - return isNaN(date) |
21 | | - ? "Invalid Date" |
22 | | - : isUTCMidnight(date) |
23 | | - ? formatYear(date.getUTCFullYear()) + "-" + pad(date.getUTCMonth() + 1, 2) + "-" + pad(date.getUTCDate(), 2) |
24 | | - : formatYear(date.getFullYear()) + "-" + pad(date.getMonth() + 1, 2) + "-" + pad(date.getDate(), 2) |
25 | | - + "T" + pad(date.getHours(), 2) + ":" + pad(date.getMinutes(), 2) |
26 | | - + (date.getMilliseconds() ? ":" + pad(date.getSeconds(), 2) + "." + pad(date.getMilliseconds(), 3) |
27 | | - : date.getSeconds() ? ":" + pad(date.getSeconds(), 2) |
28 | | - : ""); |
| 4 | + return format(date, "Invalid Date"); |
29 | 5 | } |
0 commit comments