Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
"rollup": "^2.37.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^7.0.2"
},
"dependencies": {
"isoformat": "^0.2.0"
}
}
28 changes: 2 additions & 26 deletions src/formatDate.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
function pad(value, width) {
var s = value + "", length = s.length;
return length < width ? new Array(width - length + 1).join(0) + s : s;
}

function isUTCMidnight(date) {
return date.getUTCMilliseconds() === 0
&& date.getUTCSeconds() === 0
&& date.getUTCMinutes() === 0
&& date.getUTCHours() === 0;
}

function formatYear(year) {
return year < 0 ? "-" + pad(-year, 6)
: year > 9999 ? "+" + pad(year, 6)
: pad(year, 4);
}
import {format} from "isoformat";

export default function formatDate(date) {
return isNaN(date)
? "Invalid Date"
: isUTCMidnight(date)
? formatYear(date.getUTCFullYear()) + "-" + pad(date.getUTCMonth() + 1, 2) + "-" + pad(date.getUTCDate(), 2)
: formatYear(date.getFullYear()) + "-" + pad(date.getMonth() + 1, 2) + "-" + pad(date.getDate(), 2)
+ "T" + pad(date.getHours(), 2) + ":" + pad(date.getMinutes(), 2)
+ (date.getMilliseconds() ? ":" + pad(date.getSeconds(), 2) + "." + pad(date.getMilliseconds(), 3)
: date.getSeconds() ? ":" + pad(date.getSeconds(), 2)
: "");
return format(date, "Invalid Date");
}
Loading