Skip to content

getFormatDateInfo should say if pattern is a date or not #77

@borgar

Description

@borgar

Ideally, the output structure should emit a type field to show if pattern is really a date or not. Something like this:

type FormatDateInfo = {
  // new date type field
  type: "date" | "time" | "datetime" | "none",
  clockType: 12 | 24,
  seconds: boolean,
  minutes: boolean,
  hours: boolean,
  day: boolean,
  month: boolean,
  year: boolean,
}

This has the benefit that:

  1. You don't have to call isDateFormat to determine if something is a date beforehand.
  2. You can pass the info.time into a map of default patterns:
const ISO_PATTERNS = {
  date: "yyyy-mm-dd",
  time: "hh:mm:ss",
  datetime: "yyyy-mm-ddThh:mm:ss",
  none: "General",
};

// return an ISO formatted date, or a general formatted number, based on a cell format pattern:
const info = getFormatDateInfo(pattern);
const normalizedPattern = ISO_PATTERNS[info.type];
return format(normalizedPattern, value);

Alternatively, we might consider a isDate + granularity approach:

type FormatDateInfo = {
  // new "is a date or not" field
  date: boolean,
  // new granularity field
  granularity: "year" | "month" | "day" | "hours" | "minutes" | "seconds" | "none",
  ...
}

This does make it impossible to differentiate the "time" type though, so maybe it makes sense to do both.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions