{"record":{"id":"9c5d8a1df87e7c8a","repo":"parallax/jsPDF","slug":"invalid-argument-passed-to-jspdf-setcreationdate","errorCode":null,"errorMessage":"Invalid argument passed to jsPDF.setCreationDate","messagePattern":"Invalid argument passed to jsPDF\\.setCreationDate","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":685,"sourceCode":"    // var timeZoneMinutes = parseInt(parmPDFDate.substr(20, 2), 10);\n\n    var resultingDate = new Date(year, month, date, hour, minutes, seconds, 0);\n    return resultingDate;\n  });\n\n  var setCreationDate = (API.__private__.setCreationDate = function(date) {\n    var tmpCreationDateString;\n    var regexPDFCreationDate = /^D:(20[0-2][0-9]|203[0-7]|19[7-9][0-9])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])(0[0-9]|1[0-9]|2[0-3])(0[0-9]|[1-5][0-9])(0[0-9]|[1-5][0-9])(\\+0[0-9]|\\+1[0-4]|-0[0-9]|-1[0-1])'(0[0-9]|[1-5][0-9])'?$/;\n    if (typeof date === \"undefined\") {\n      date = new Date();\n    }\n\n    if (date instanceof Date) {\n      tmpCreationDateString = convertDateToPDFDate(date);\n    } else if (regexPDFCreationDate.test(date)) {\n      tmpCreationDateString = date;\n    } else {\n      throw new Error(\"Invalid argument passed to jsPDF.setCreationDate\");\n    }\n    creationDate = tmpCreationDateString;\n    return creationDate;\n  });\n\n  var getCreationDate = (API.__private__.getCreationDate = function(type) {\n    var result = creationDate;\n    if (type === \"jsDate\") {\n      result = convertPDFDateToDate(creationDate);\n    }\n    return result;\n  });\n\n  /**\n   * @name setCreationDate\n   * @memberof jsPDF#\n   * @function\n   * @instance","sourceCodeStart":667,"sourceCodeEnd":703,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L667-L703","documentation":"`setCreationDate` (src/jspdf.js:673) accepts three forms: no argument (defaults to now), a `Date` instance (converted via convertDateToPDFDate at src/jspdf.js:634), or a string matching the PDF date regex `^D:YYYYMMDDHHmmSS±HH'mm'?` (src/jspdf.js:675). Anything else throws at src/jspdf.js:685. The regex is strict — it requires the `D:` prefix, a year in 1970–2037, and the timezone suffix in the `+HH'mm'` / `-HH'mm'` form.","triggerScenarios":"`doc.setCreationDate('2023-01-01')` (ISO string, no D: prefix); `doc.setCreationDate('D:2023')` (too short); `doc.setCreationDate(1672531200000)` (a number, not a Date); `doc.setCreationDate('D:2099...')` (year out of the 1970–2037 range).","commonSituations":"Passing a database/ISO timestamp string directly instead of `new Date(isoString)`; copying a PDF metadata string but dropping the `D:` prefix; year-2037 boundary issues for far-future dated documents; numeric epoch timestamps passed as-is.","solutions":["Pass a `Date` object and let jsPDF format it: `doc.setCreationDate(new Date('2023-01-01'))`.","If you must pass a string, build it in the exact `D:YYYYMMDDHHmmSS+HH'mm'` format the regex expects.","For epoch numbers, wrap in `new Date(ms)` first.","Avoid years outside 1970–2037 (the regex caps at 2037)."],"exampleFix":"// before\n doc.setCreationDate('2023-01-01T00:00:00Z');  // throws\n\n// after\n doc.setCreationDate(new Date('2023-01-01T00:00:00Z'));","handlingStrategy":"validation","validationCode":"function toPdfDate(input) {\n  if (input == null) return new Date();          // default now\n  if (input instanceof Date) return input;        // Date instance\n  if (typeof input === 'number') return new Date(input); // epoch ms\n  if (typeof input === 'string' && /^D:\\d{14}/.test(input)) return input; // already PDF date\n  // anything else (incl. ISO strings) -> convert via Date\n  var d = new Date(input);\n  return isNaN(d.getTime()) ? null : d;\n}\nvar d = toPdfDate(value);\nif (d) doc.setCreationDate(d); else /* handle invalid */","typeGuard":"function isPdfDateOrConvertible(v) {\n  if (v == null || v instanceof Date) return true;\n  if (typeof v === 'number') return true;\n  if (typeof v === 'string') return /^D:(19[7-9]\\d|20[0-3]\\d)\\d{10}/.test(v) || !isNaN(new Date(v).getTime());\n  return false;\n}","tryCatchPattern":"try {\n  doc.setCreationDate(value);\n} catch (e) {\n  if (/setCreationDate/.test(e.message)) {\n    doc.setCreationDate(new Date(value));  // retry via Date object\n  } else throw e;\n}","preventionTips":["Always pass a Date object rather than a raw string.","For ISO/epoch timestamps, wrap in new Date() first.","Avoid years outside 1970-2037; the internal regex caps the range."],"tags":["metadata","date","creationdate","validation","regex"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}