Skip to content

Commit 706ae29

Browse files
authored
adopt isoformat for dates (#173)
* adopt isoformat for dates * update dependencies
1 parent b2d18d2 commit 706ae29

File tree

3 files changed

+654
-776
lines changed

3 files changed

+654
-776
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@
3939
"rollup": "^2.37.1",
4040
"rollup-plugin-node-resolve": "^5.2.0",
4141
"rollup-plugin-terser": "^7.0.2"
42+
},
43+
"dependencies": {
44+
"isoformat": "^0.2.0"
4245
}
4346
}

src/formatDate.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
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";
182

193
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");
295
}

0 commit comments

Comments
 (0)