parse-date-text.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * A date typed by hand, read the way the workshop writes dates. Shared by the
  3. * date field and the other fields that take a typed date, so every one of
  4. * them reads "03/04/2019" the same way.
  5. */
  6. /** A local date as its day key, "2026-05-25". */
  7. export function toISODate(date: Date): string {
  8. return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
  9. }
  10. // Sane bounds for any date a workshop would ever type. Everything outside is
  11. // rejected on every parse path, so garbage can never reach the form value.
  12. const MIN_YEAR = 1900
  13. const MAX_YEAR = 2100
  14. /** Build a date from parts; undefined on overflow (32.13.…) or absurd years */
  15. function buildDate(year: number, month: number, day: number): Date | undefined {
  16. if (year < MIN_YEAR || year > MAX_YEAR) return undefined
  17. const d = new Date(year, month - 1, day)
  18. return d.getFullYear() === year && d.getMonth() === month - 1 && d.getDate() === day
  19. ? d
  20. : undefined
  21. }
  22. /**
  23. * Parse typed text into a Date. Supports separated numerics (25.05.2026,
  24. * 25/05/2026, 2026-05-25) with dot/slash/dash/space, compact digits
  25. * (25052026, 250526) and longhand text ("February 01, 2001"). Day-vs-month
  26. * order follows the workshop date format when ambiguous; a value over 12 is
  27. * always the day. Years outside 1900-2100 never parse.
  28. */
  29. export function parseDateText(raw: string, dateFormat: string): Date | undefined {
  30. const text = raw.trim()
  31. if (!text) return undefined
  32. const monthFirst = dateFormat.trimStart().startsWith('M')
  33. const yearFirst = /^y/i.test(dateFormat.trimStart())
  34. const m = text.match(/^(\d{1,4})[./\-\s](\d{1,2})[./\-\s](\d{1,4})$/)
  35. if (m) {
  36. const [a, b, c] = [Number(m[1]), Number(m[2]), Number(m[3])]
  37. if (m[1].length === 4) return buildDate(a, b, c) // 2026-05-25
  38. const year = c < 100 ? 2000 + c : c
  39. if (a > 12 && b <= 12) return buildDate(year, b, a)
  40. if (b > 12 && a <= 12) return buildDate(year, a, b)
  41. return monthFirst ? buildDate(year, a, b) : buildDate(year, b, a)
  42. }
  43. // Compact digits: 25052026 / 05252026 / 20260525 per format, 250526 short
  44. if (/^\d{8}$/.test(text)) {
  45. if (yearFirst) return buildDate(+text.slice(0, 4), +text.slice(4, 6), +text.slice(6, 8))
  46. const p1 = +text.slice(0, 2)
  47. const p2 = +text.slice(2, 4)
  48. const year = +text.slice(4, 8)
  49. const first = monthFirst ? buildDate(year, p1, p2) : buildDate(year, p2, p1)
  50. return (
  51. first ??
  52. (monthFirst ? buildDate(year, p2, p1) : buildDate(year, p1, p2)) ??
  53. buildDate(+text.slice(0, 4), +text.slice(4, 6), +text.slice(6, 8))
  54. )
  55. }
  56. if (/^\d{6}$/.test(text)) {
  57. const p1 = +text.slice(0, 2)
  58. const p2 = +text.slice(2, 4)
  59. const year = 2000 + +text.slice(4, 6)
  60. return monthFirst ? buildDate(year, p1, p2) : buildDate(year, p2, p1)
  61. }
  62. // Any other bare digit run is ambiguous mid-typing input: never guess
  63. if (/^[\d./\-\s]*$/.test(text)) return undefined
  64. // Longhand text ("February 01, 2001"), same year bounds
  65. const d = new Date(text)
  66. if (isNaN(d.getTime())) return undefined
  67. return d.getFullYear() >= MIN_YEAR && d.getFullYear() <= MAX_YEAR ? d : undefined
  68. }