Skip to content

[JS] - Parse a date in using the given format first and then try the default date parser #14692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2022
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
19 changes: 11 additions & 8 deletions src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,19 @@ class AForm {
}

_parseDate(cFormat, cDate) {
const ddate = Date.parse(cDate);
if (isNaN(ddate)) {
try {
return this._util.scand(cFormat, cDate);
} catch (error) {
return null;
let date = null;
try {
date = this._util.scand(cFormat, cDate);
} catch (error) {}
if (!date) {
date = Date.parse(cDate);
if (isNaN(date)) {
date = null;
} else {
date = new Date(date);
}
} else {
return new Date(ddate);
}
return date;
}

AFMergeChange(event = globalThis.event) {
Expand Down
10 changes: 7 additions & 3 deletions src/scripting_api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ class Util extends PDFObject {
}

scand(cFormat, cDate) {
if (typeof cDate !== "string") {
return new Date(cDate);
}

if (cDate === "") {
return new Date();
}
Expand Down Expand Up @@ -536,15 +540,15 @@ class Util extends PDFObject {

const [re, actions] = this._scandCache.get(cFormat);

const matches = new RegExp(re, "g").exec(cDate);
const matches = new RegExp(`^${re}$`, "g").exec(cDate);
if (!matches || matches.length !== actions.length + 1) {
return null;
}

const data = {
year: 0,
year: 2000,
month: 0,
day: 0,
day: 1,
Comment on lines +549 to +551
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these default values changed to match the Adobe JS specification, or simply to match the implementation in Adobe Reader?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened the pdf from the issue in Acrobat and the UI for the date selection is set to January 2000 for the day field and to the 1st of December 2000 for the month field.

hours: 0,
minutes: 0,
seconds: 0,
Expand Down
15 changes: 15 additions & 0 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,21 @@ describe("Scripting", function () {
});
});

describe("AFParseDateEx", function () {
it("should parse a date with a format", async () => {
const check = async (date, format, expected) => {
const value = await myeval(
`AFParseDateEx("${date}", "${format}").toISOString()`
);
expect(value).toEqual(new Date(expected).toISOString());
};

await check("05", "dd", "2000/01/05");
await check("12", "mm", "2000/12/01");
await check("2022", "yyyy", "2022/01/01");
});
});

describe("AFExtractNums", function () {
it("should extract numbers", async () => {
let value = await myeval(`AFExtractNums("123 456 789")`);
Expand Down