{"record":{"id":"d0c9e08242b45baa","repo":"medusajs/medusa","slug":"cannot-format-date-to-iso-string-date","errorCode":null,"errorMessage":"Cannot format date to ISO string: ${date}","messagePattern":"Cannot format date to ISO string: (.+?)","errorType":"validation","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/core/utils/src/common/get-iso-string-from-date.ts","lineNumber":6,"sourceCode":"import { isDate } from \"./is-date\"\nimport { MedusaError } from \"./errors\"\n\nexport const GetIsoStringFromDate = (date: Date | string) => {\n  if (!isDate(date)) {\n    throw new MedusaError(\n      MedusaError.Types.INVALID_DATA,\n      `Cannot format date to ISO string: ${date}`\n    )\n  }\n\n  date = new Date(date)\n\n  return date.toISOString()\n}\n","sourceCodeStart":1,"sourceCodeEnd":16,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/core/utils/src/common/get-iso-string-from-date.ts#L1-L16","documentation":"GetIsoStringFromDate converts a Date or date string to an ISO string and first asserts the input is date-like via `isDate`. If the value cannot be interpreted as a date it throws MedusaError INVALID_DATA.","triggerScenarios":"Calling GetIsoStringFromDate(undefined), GetIsoStringFromDate(null), or with a malformed string like \"31/12/2024\" or \"\".","commonSituations":"Passing an optional request field straight through without a presence check; timezone/locale ambiguous date strings from user input or CSV imports.","solutions":["Check the value exists before formatting","Normalize/parse user input with an explicit date parser (e.g. new Date(...) + isNaN check) before calling","Default missing values (e.g. new Date().toISOString()) when a timestamp is required"],"exampleFix":"// before\nconst iso = GetIsoStringFromDate(body.birthdate)\n// after\nconst iso = body.birthdate ? GetIsoStringFromDate(body.birthdate) : undefined","handlingStrategy":"type-guard","validationCode":"if (value !== undefined && value !== null && !isDate(value)) throw new Error(`bad date: ${value}`)","typeGuard":"import { isDate } from \"@medusajs/framework/utils\"\nconst isDateLike = (v: unknown): v is Date | string => isDate(v as any)","tryCatchPattern":"try { GetIsoStringFromDate(v) } catch (e) { if (e.type === 'invalid_data') fallback to default/omit; else throw e }","preventionTips":["Trim and presence-check optional date inputs","Validate dates at the API boundary with zod coerce.date()"],"tags":["date","validation","utils"],"backgroundTag":"invalid-date-format","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}