{"record":{"id":"e890ccd691d7c6f7","repo":"we-promise/sure","slug":"invalid-date-format-for-row-date-e-message","errorCode":null,"errorMessage":"Invalid date format for '#{row.date}': #{e.message}","messagePattern":"Invalid date format for '#(.+?)': #(.+?)","errorType":"validation","errorClass":"AccountImport::OpeningBalanceError","httpStatus":null,"severity":"error","filePath":"app/models/account_import.rb","lineNumber":27,"sourceCode":"\n        account = family.accounts.build(\n          name: row.name,\n          balance: row.amount.to_d,\n          currency: row.currency,\n          accountable: accountable_class.new,\n          import: self\n        )\n\n        account.save!\n\n        manager = Account::OpeningBalanceManager.new(account)\n\n        # Parse date if provided, otherwise use default\n        balance_date = if row.date.present?\n          begin\n            Date.strptime(row.date, date_format)\n          rescue ArgumentError => e\n            raise OpeningBalanceError, \"Invalid date format for '#{row.date}': #{e.message}\"\n          end\n        else\n          nil\n        end\n\n        result = manager.set_opening_balance(balance: row.amount.to_d, date: balance_date)\n\n        # Re-raise since we should never have an error here\n        if result.error\n          raise OpeningBalanceError, result.error\n        end\n      end\n    end\n  end\n\n  def mapping_steps\n    [ Import::AccountTypeMapping ]\n  end","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/we-promise/sure/blob/e69894adb92547273377398c15f45c979cd9416a/app/models/account_import.rb#L9-L45","documentation":"During an account import (CSV), each row may carry an opening-balance date parsed with Date.strptime(row.date, date_format) using the format the user selected for the import. When strptime cannot match the cell against that format it raises ArgumentError, which the importer wraps in AccountImport::OpeningBalanceError with the offending value and strptime's message. The error is row-specific: the amount/account parts may be fine; only the date cell and the declared format disagree.","triggerScenarios":"Selecting %d-%m-%Y in the import UI while the CSV column contains 2024-01-31 (ISO); a cell with a textual month (\"Jan 31, 2024\") against %m/%d/%Y; two-digit year vs four-digit format mismatch (%y vs %Y); stray whitespace or a Unicode non-breaking space in the date cell; an Excel export producing 01/02/2024 while the format was set for a different regional order.","commonSituations":"Bank CSV exports whose date order differs from the user's locale assumption (US mm/dd vs EU dd/mm); switching banks and reusing the previous import settings; spreadsheets that silently reformat date columns on save; a single malformed row in an otherwise good export (one bank glitch row blocks the import since save! and the raise abort the run).","solutions":["Re-run the import with the date format that matches the actual column (open the CSV in a text editor, not Excel, to see raw cells)","Fix or delete the offending date cell(s) — the error message names the exact value; search the file for it","Normalize dates upstream: preprocess the CSV to ISO 8601 (YYYY-MM-DD) and pick that format in the UI","If exporting from Excel, format the column as text or as the exact pattern chosen in the importer before saving"],"exampleFix":"# before\nDate.strptime(\"2024-01-31\", \"%d-%m-%Y\")\n# => ArgumentError: invalid date -> OpeningBalanceError \"Invalid date format for '2024-01-31': ...\"\n\n# after\nDate.strptime(\"2024-01-31\", \"%Y-%m-%d\") # pick the matching format in the import UI\n# or leave the date column blank to use the default (balance_date = nil path)","handlingStrategy":"validation","validationCode":"# Before running the import, dry-run the date column\nfmt = import.date_format # e.g. \"%d-%m-%Y\"\nbad = csv_rows.reject { |r| r.date.blank? || Date.strptime(r.date, fmt) rescue false }\nabort \"#{bad.size} rows fail the selected format: #{bad.first(3).map(&:date)}\" if bad.any?","typeGuard":"def parseable_date?(cell, format)\n  return true if cell.to_s.strip.empty?\n  !!Date.strptime(cell.to_s.strip, format)\nrescue ArgumentError, TypeError\n  false\nend","tryCatchPattern":"rescue AccountImport::OpeningBalanceError => e\n  # e.message names the exact offending value; fix that row/format and re-import\n  render json: { error: e.message }, status: :unprocessable_entity\nend","preventionTips":["Open CSVs in a text editor to see the true date format before choosing one in the UI","Prefer normalizing dates to ISO 8601 (YYYY-MM-DD) in a preprocessing step","Watch locale order traps: 03/04/2024 is ambiguous between %d/%m and %m/%d","Check for stray spaces/Unicode dashes in exported cells; clean them upstream"],"tags":["rails","csv-import","date-parsing","strptime","data-validation"],"backgroundTag":"date-parsing-failed","analyzedSha":"e69894adb92547273377398c15f45c979cd9416a","analyzedAt":"2026-08-21T18:22:41.165Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}