medusajs/medusa · error · MedusaError

Cannot format date to ISO string: ${date}

Error message

Cannot format date to ISO string: ${date}

What it means

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.

Source

Thrown at packages/core/utils/src/common/get-iso-string-from-date.ts:6

import { isDate } from "./is-date"
import { MedusaError } from "./errors"

export const GetIsoStringFromDate = (date: Date | string) => {
  if (!isDate(date)) {
    throw new MedusaError(
      MedusaError.Types.INVALID_DATA,
      `Cannot format date to ISO string: ${date}`
    )
  }

  date = new Date(date)

  return date.toISOString()
}

View on GitHub (pinned to 5e06e544a2)

Solutions

  1. Check the value exists before formatting
  2. Normalize/parse user input with an explicit date parser (e.g. new Date(...) + isNaN check) before calling
  3. Default missing values (e.g. new Date().toISOString()) when a timestamp is required

Example fix

// before
const iso = GetIsoStringFromDate(body.birthdate)
// after
const iso = body.birthdate ? GetIsoStringFromDate(body.birthdate) : undefined
Defensive patterns

Strategy: type-guard

Validate before calling

if (value !== undefined && value !== null && !isDate(value)) throw new Error(`bad date: ${value}`)

Type guard

import { isDate } from "@medusajs/framework/utils"
const isDateLike = (v: unknown): v is Date | string => isDate(v as any)

Try / catch

try { GetIsoStringFromDate(v) } catch (e) { if (e.type === 'invalid_data') fallback to default/omit; else throw e }

Prevention

When it happens

Trigger: Calling GetIsoStringFromDate(undefined), GetIsoStringFromDate(null), or with a malformed string like "31/12/2024" or "".

Common situations: Passing an optional request field straight through without a presence check; timezone/locale ambiguous date strings from user input or CSV imports.

Related errors


AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27). Data as JSON: /api/errors/d0c9e08242b45baa. Report an issue: GitHub.