Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1.05 KB

README.md

File metadata and controls

46 lines (34 loc) · 1.05 KB

DateFormat TS

Format date information at the type-level, according to the Unicode #35 standard


// Init date
type Date = FromIso<"2024-06-10T04:39:46.229Z">;

// Format date
type Formatted = Format<Date, "dd-MM-yy H:mm:ss aa">;
//   ^? 10-06-24 4:39:46 AM

This is just a toy project, it's probably not super useful in production

Usage

Given a DateObject

export type DateObject = {
    dayOfMonth: number;
    dayOfYear: number;
    dayOfWeek: number;
    monthOfYear: number;
    hourOfDay: number;
    minuteOfHour: number;
    secondOfMinute: number;
    year: number;
};

We can format the date information given a format string.

type FormattedDate = Format<SomeDateObject, "some-format-string">;

Limitations

  • Not all format parts implemented, like ISO and locale-stuff
  • No input validation is done
  • Escaping syntax is not respected (not implemented yet)
  • FromIso does not produce dayOfYear or dayOfWeek information