date-io

Abstraction over common javascript date management libraries

README

date-io


Stand With Ukraine

Abstraction over common JavaScript date management libraries.

npm package
codecov
typescript
travis
lerna
code style: prettier

The project exposes an abstraction interface over luxon, date-fns v2, dayjs and moment.
It allows you to build any UI date or time components, while utilizing the same date management library in use within your user's project.

It simplifies timezone management, allows your code to return the exact same type that your user expects and works with specific calendar systems (e.g. Jalali calendar)

Projects


LibraryDownloads
------------------------------------------------------------------------------------------------------------------------------------------------------------:
@date-io/date-fns[![npm
@date-io/moment[![npm
@date-io/luxon[![npm
@date-io/dayjs[![npm
@date-io/js-joda[![npm
@date-io/date-fns-jalali[![npm
@date-io/jalaali[![npm
@date-io/hijri[![npm

Usage example


  1. ```js
  2. import LuxonAdapter from "@date-io/luxon";
  3. import DateFnsAdapter from "@date-io/date-fns";

  4. const dateFns = new DateFnsAdapter();
  5. const luxon = new LuxonAdapter({ locale: "fr" }); // pass french locale

  6. const initialLuxonDate = luxon.date("2018-10-28T11:44:00.000Z");
  7. const initialDateFnsDate = dateFns.date("2018-10-28T11:44:00.000Z");

  8. const updatedLuxonDate = luxon.addDays(initialLuxonDate, 2);
  9. const updatedDateFnsDate = dateFns.addDays(initialDateFnsDate, 2);

  10. luxon.format(updatedLuxonDate, "fullDateTime24h"); // "2018, octobre 30 11:44"
  11. dateFns.format(updatedLuxonDate, "fullDateTime24h"); // "2018, October 30th 11:44"
  12. ```

Interface


The implemented interface. If you cannot find the method you require, please let us know, and we will add it!

Localized output will of course vary based on the locale and date library used. Inline examples here are based on using
moment with the en-US locale.

  1. ```tsx
  2. export interface DateIOFormats<TLibFormatToken = string> {
  3.   /** Localized full date @example "Jan 1, 2019" */
  4.   fullDate: TLibFormatToken;
  5.   /** Partially localized full date with weekday, useful for text-to-speech accessibility @example "Tuesday, January 1, 2019" */
  6.   fullDateWithWeekday: TLibFormatToken;
  7.   /** Date format string with month and day of month @example "1 January" */
  8.   normalDate: TLibFormatToken;
  9.   /** Date format string with weekday, month and day of month @example "Wed, Jan 1" */
  10.   normalDateWithWeekday: TLibFormatToken;
  11.   /** Shorter day format @example "Jan 1" */
  12.   shortDate: TLibFormatToken;
  13.   /** Year format string @example "2019" */
  14.   year: TLibFormatToken;
  15.   /** Month format string @example "January" */
  16.   month: TLibFormatToken;
  17.   /** Short month format string @example "Jan" */
  18.   monthShort: TLibFormatToken;
  19.   /** Month with year format string @example "January 2018" */
  20.   monthAndYear: TLibFormatToken;
  21.   /** Month with date format string @example "January 1" */
  22.   monthAndDate: TLibFormatToken;
  23.   /** Weekday format string @example "Wednesday" */
  24.   weekday: TLibFormatToken;
  25.   /** Short weekday format string @example "Wed" */
  26.   weekdayShort: TLibFormatToken;
  27.   /** Day format string @example "1" */
  28.   dayOfMonth: TLibFormatToken;
  29.   /** Hours format string @example "11" */
  30.   hours12h: TLibFormatToken;
  31.   /** Hours format string @example "23" */
  32.   hours24h: TLibFormatToken;
  33.   /** Minutes format string @example "44" */
  34.   minutes: TLibFormatToken;
  35.   /** Seconds format string @example "00" */
  36.   seconds: TLibFormatToken;
  37.   /** Full time localized format string @example "11:44 PM" for US, "23:44" for Europe */
  38.   fullTime: TLibFormatToken;
  39.   /** Not localized full time format string @example "11:44 PM" */
  40.   fullTime12h: TLibFormatToken;
  41.   /** Not localized full time format string @example "23:44" */
  42.   fullTime24h: TLibFormatToken;
  43.   /** Date & time format string with localized time @example "Jan 1, 2018 11:44 PM" */
  44.   fullDateTime: TLibFormatToken;
  45.   /** Not localized date & Time format 12h @example "Jan 1, 2018 11:44 PM" */
  46.   fullDateTime12h: TLibFormatToken;
  47.   /** Not localized date & Time format 24h @example "Jan 1, 2018 23:44" */
  48.   fullDateTime24h: TLibFormatToken;
  49.   /** Localized keyboard input friendly date format @example "02/13/2020 */
  50.   keyboardDate: TLibFormatToken;
  51.   /** Localized keyboard input friendly date/time format @example "02/13/2020 23:44" */
  52.   keyboardDateTime: TLibFormatToken;
  53.   /** Partially localized keyboard input friendly date/time 12h format @example "02/13/2020 11:44 PM" */
  54.   keyboardDateTime12h: TLibFormatToken;
  55.   /** Partially localized keyboard input friendly date/time 24h format @example "02/13/2020 23:44" */
  56.   keyboardDateTime24h: TLibFormatToken;
  57. }

  58. export type Unit =
  59.   | "years"
  60.   | "quarters"
  61.   | "months"
  62.   | "weeks"
  63.   | "days"
  64.   | "hours"
  65.   | "minutes"
  66.   | "seconds"
  67.   | "milliseconds";

  68. export interface IUtils<TDate> {
  69.   formats: DateIOFormats<any>;
  70.   locale?: any;
  71.   moment?: any;
  72.   dayjs?: any;
  73.   /** Name of the library that is used right now */
  74.   lib: string;

  75.   // constructor (options?: { formats?: DateIOFormats, locale?: any, instance?: any });

  76.   date(value?: any): TDate | null;
  77.   toJsDate(value: TDate): Date;
  78.   parseISO(isString: string): TDate;
  79.   toISO(value: TDate): string;
  80.   parse(value: string, format: string): TDate | null;

  81.   getCurrentLocaleCode(): string;
  82.   is12HourCycleInCurrentLocale(): boolean;
  83.   /** Returns user readable format (taking into account localized format tokens), useful to render helper text for input (e.g. placeholder). For luxon always returns empty string. */
  84.   getFormatHelperText(format: string): string;

  85.   isNull(value: TDate | null): boolean;
  86.   isValid(value: any): boolean;
  87.   getDiff(value: TDate, comparing: TDate | string, unit?: Unit): number;
  88.   isEqual(value: any, comparing: any): boolean;

  89.   isSameDay(value: TDate, comparing: TDate): boolean;
  90.   isSameMonth(value: TDate, comparing: TDate): boolean;
  91.   isSameYear(value: TDate, comparing: TDate): boolean;
  92.   isSameHour(value: TDate, comparing: TDate): boolean;

  93.   isAfter(value: TDate, comparing: TDate): boolean;
  94.   isAfterDay(value: TDate, comparing: TDate): boolean;
  95.   isAfterYear(value: TDate, comparing: TDate): boolean;

  96.   isBeforeDay(value: TDate, comparing: TDate): boolean;
  97.   isBeforeYear(value: TDate, comparing: TDate): boolean;
  98.   isBefore(value: TDate, comparing: TDate): boolean;

  99.   isWithinRange(value: TDate, range: [TDate, TDate]): boolean;

  100.   startOfYear(value: TDate): TDate;
  101.   endOfYear(value: TDate): TDate;
  102.   startOfMonth(value: TDate): TDate;
  103.   endOfMonth(value: TDate): TDate;
  104.   startOfWeek(value: TDate): TDate;
  105.   endOfWeek(value: TDate): TDate;

  106.   addSeconds(value: TDate, count: number): TDate;
  107.   addMinutes(value: TDate, count: number): TDate;
  108.   addHours(value: TDate, count: number): TDate;
  109.   addDays(value: TDate, count: number): TDate;
  110.   addWeeks(value: TDate, count: number): TDate;
  111.   addMonths(value: TDate, count: number): TDate;
  112.   addYears(value: TDate, count: number): TDate;

  113.   startOfDay(value: TDate): TDate;
  114.   endOfDay(value: TDate): TDate;

  115.   format(value: TDate, formatKey: keyof DateIOFormats): string;
  116.   formatByString(value: TDate, formatString: string): string;
  117.   formatNumber(numberToFormat: string): string;

  118.   getHours(value: TDate): number;
  119.   setHours(value: TDate, count: number): TDate;

  120.   getMinutes(value: TDate): number;
  121.   setMinutes(value: TDate, count: number): TDate;

  122.   getSeconds(value: TDate): number;
  123.   setSeconds(value: TDate, count: number): TDate;

  124.   getDate(value: TDate): number;
  125.   setDate(value: TDate, count: number): TDate;

  126.   getMonth(value: TDate): number;
  127.   getDaysInMonth(value: TDate): number;
  128.   setMonth(value: TDate, count: number): TDate;
  129.   getNextMonth(value: TDate): TDate;
  130.   getPreviousMonth(value: TDate): TDate;

  131.   getMonthArray(value: TDate): TDate[];

  132.   getYear(value: TDate): number;
  133.   setYear(value: TDate, count: number): TDate;

  134.   mergeDateAndTime(date: TDate, time: TDate): TDate;

  135.   getWeekdays(): string[];
  136.   getWeekArray(date: TDate): TDate[][];
  137.   getYearRange(start: TDate, end: TDate): TDate[];

  138.   /** Allow to customize displaying "am/pm" strings */
  139.   getMeridiemText(ampm: "am" | "pm"): string;
  140. }
  141. ```

For library authors


If you are a library author that exposes date/time management utils or controls you may want to use date-io to interop with the most popular libraries. Here are some instructions of how to use date-fns as an adapter.

1. Install the bindings


First of all it is required to provide the adapters for your users. We do not recommend to install the date-io directly by the end users, cause it may be easy to mismatch the version. The better way will be to reexport them.

Firstly install all the adapters you want to support and lock the version.

Yes, you will install all of them as dependencies. But every adapter is 50kb unpacked npm module and relieas to the library as for optional peer dependency. It won't be included in user bundle until user will choose which library he want's to use.


  1. ```json
  2. {
  3.   "dependencies": {
  4.     "@date-io/date-fns": "x.x.x",
  5.     "@date-io/dayjs": "x.x.x",
  6.     "@date-io/luxon": "x.x.x",
  7.     "@date-io/date-fns-jalali": "x.x.x",
  8.     "@date-io/jalaali": "x.x.x"
  9.   }
  10. }
  11. ```

2. Reexport the bindings


  1. ```js
  2. // you-awesome-lib/adapters/date-fns
  3. export { default } from "@date-io/date-fns";
  4. ```

You can also manually extend the adapter if you need to. Create a custom interface:

  1. ```ts
  2. // your-awesome-lib/adapters/CustomAdapter
  3. import { IUtils } from "@date-io/core/IUtils";

  4. interface CustomUtils<TDate> extends IUtils<TDate> {
  5.   getDayOfYear(day: TDate): number;
  6. }
  7. ```

And extend date-io classes implementing your custom interface:

  1. ```ts
  2. // you-awesome-lib/adapters/date-fns
  3. import { CustomUtils } from "../CustomUtils";
  4. import getDayOfYear from "date-fns/getDayOfYear";
  5. import DateIODateFnsAdapter from "@date-io/date-fns";

  6. export const createMyAdapter(options) {
  7.   const dateFns = new DateFnsUtils(options)

  8.   const adapter = {
  9.     ...dateFns,

  10.     getWeekArray() {
  11.       const startDate = endOfWeek(adapter.endOfMonth(date), { locale: adapter.locale })
  12.       const extraWeek = adapter.getWeekdays().map((day, index) => adapter.addDays(startDate, index + 1))
  13.       return [...dateFns.getWeekArray(date), extraWeek]
  14.     },

  15.     // ...
  16.   }

  17.   return adapter
  18. }
  19. ```

3. Use it for date-time management


Register it using your library context. It may be react context, dependency injection container or any other tool that allow user to register the used library 1 time.

  1. ```tsx
  2. // react example
  3. import { createMyAdapter } from "your-awesome-lib/adapters/date-fns";

  4. <DateLibProvider adapter={createMyAdapter({ locale: "fr" })}>{/* ... */}</DateLibProvider>;
  5. ```

And use the interface of date-io (or your custom interface).

  1. ```ts
  2. import { IUtils } from "@date-io/core/IUtils";

  3. function myFunctionInLibrary<TDate>(date: TDate, adapter: IUtils<TDate>) {
  4.   // ...
  5.   const weekArray = adapter.getWeekArray(Date);
  6.   // ...
  7. }
  8. ```