diff --git a/spec/index.js b/spec/index.js index 3c25872..07d647a 100644 --- a/spec/index.js +++ b/spec/index.js @@ -91,6 +91,14 @@ describe("Node-Passkit-generator", function () { expect(noTimeZoneDateTime).toBe("2021-04-10T00:00:00"); }); + it("A date as a Date object will apply changes", () => { + pass.expiration(new Date(2020,5,1,0,0,0)); + // this is made to avoid problems with winter and summer time: + // we focus only on the date and time for the tests. + let noTimeZoneDateTime = pass._props["expirationDate"].split("+")[0]; + expect(noTimeZoneDateTime).toBe("2020-06-01T00:00:00"); + }); + it("An invalid date, will not apply changes", () => { pass.expiration("32/18/228317"); expect(pass._props["expirationDate"]).toBe(undefined); @@ -125,6 +133,14 @@ describe("Node-Passkit-generator", function () { let noTimeZoneDateTime = pass._props["relevantDate"].split("+")[0]; expect(noTimeZoneDateTime).toBe("2021-04-10T00:00:00"); }); + + it("A date as a Date object will apply changes", () => { + pass.relevance("relevantDate",new Date(2020,5,1,0,0,0)); + // this is made to avoid problems with winter and summer time: + // we focus only on the date and time for the tests. + let noTimeZoneDateTime = pass._props["relevantDate"].split("+")[0]; + expect(noTimeZoneDateTime).toBe("2020-06-01T00:00:00"); + }); }); describe("relevance('maxDistance')", () => { diff --git a/src/pass.js b/src/pass.js index c6a802d..437e852 100644 --- a/src/pass.js +++ b/src/pass.js @@ -259,7 +259,7 @@ class Pass { */ expiration(date, format) { - if (typeof date !== "string") { + if (typeof date !== "string" && !(date instanceof Date)) { return this; } @@ -327,6 +327,11 @@ class Pass { return assignLength(Number(!cond), this); } else if (type === "relevantDate") { + if (typeof data !== "string" && !(data instanceof Date)) { + genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date")); + return this; + } + let dateParse = dateToW3CString(data, relevanceDateFormat); if (!dateParse) { diff --git a/src/utils.js b/src/utils.js index 56a1aeb..61c7504 100644 --- a/src/utils.js +++ b/src/utils.js @@ -34,11 +34,11 @@ function isValidRGB(value) { */ function dateToW3CString(date, format) { - if (typeof date !== "string") { + if (typeof date !== "string" && !(date instanceof Date)) { return ""; } - const parsedDate = moment(date.replace(/\//g, "-"), format || ["MM-DD-YYYY hh:mm:ss", "DD-MM-YYYY hh:mm:ss"]).format(); + const parsedDate = date instanceof Date ? moment(date).format() : moment(date.replace(/\//g, "-"), format || ["MM-DD-YYYY hh:mm:ss", "DD-MM-YYYY hh:mm:ss"]).format(); if (parsedDate === "Invalid date") { return undefined;