section-2
Version:
A library for calculating unsocial hours entitlements under the NHS agenda for change's section 2
36 lines (35 loc) • 1.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addDaysToTimestamp = exports.formatDate = void 0;
const validateTimestamp_1 = require("./validateTimestamp");
const regExpDate = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
const formatDate = (dt, format) => {
// takes a string or date and returns a formatted string
if (dt &&
(typeof dt === "number" ||
(0, validateTimestamp_1.validateTimestamp)(dt) ||
(typeof dt === "string" && regExpDate.test(dt)))) {
const jsDate = typeof dt === "string" || typeof dt === "number"
? new Date(dt)
: dt;
switch (format) {
case "sorting":
case "yyyy-mm-dd":
return jsDate
.toLocaleDateString("en-GB", {
day: "2-digit",
month: "2-digit",
year: "numeric",
})
.split("/")
.reverse()
.join("-");
}
}
return "";
};
exports.formatDate = formatDate;
const addDaysToTimestamp = (date = "", days = 0) => {
return new Date(new Date(date).getTime() + days * 24 * 60 * 60 * 1000);
};
exports.addDaysToTimestamp = addDaysToTimestamp;