Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

date-io

> 编程语言
Open source

Abstraction over common javascript date management libraries

755 stars0 likes1 views
WebsiteGitHub

About

Abstraction over common javascript date management libraries

date-io

Abstraction over common JavaScript date management libraries. The project exposes an abstraction interface over luxon, date-fns v4, 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

Library Downloads
@date-io/date-fns
@date-io/moment
@date-io/luxon
@date-io/dayjs
@date-io/js-joda
@date-io/date-fns-jalali
@date-io/jalaali
@date-io/hijri

Usage example

…

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.

…

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 on 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 installing 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.

json
{
  "dependencies": {
    "@date-io/date-fns": "x.x.x",
    "@date-io/dayjs": "x.x.x",
    "@date-io/luxon": "x.x.x",
    "@date-io/date-fns-jalali": "x.x.x",
    "@date-io/jalaali": "x.x.x"
  }
}

2. Reexport the bindings

javascript
// you-awesome-lib/adapters/date-fns
export { default } from "@date-io/date-fns";

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 allows the user to register the used library 1 time.

typescript
// react example
import { createMyAdapter } from "your-awesome-lib/adapters/date-fns";

  {/* ... */}
;

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

typescript
import { IUtils } from "@date-io/core/IUtils";

function myFunctionInLibrary(date: TDate, adapter: IUtils) {
  // ...
  const weekArray = adapter.getWeekArray(Date);
  // ...
}

Overriding behavior

It is possible to change or extend the behavior of any adapter by simply inheriting and overriding the base class of utils while saving the same interface.

Let's say you want to override getYear to always return 2007 and getWeekdays to contain only 2 weekends (let's admit that this is what we all desire) you can do the following:

typescript
class CustomDateTime extends DayjsUtils implements IUtils {
  getWeekdays = () => {
    const start = this.dayjs().startOf("week");
    return [0, 1].map((diff) => this.formatByString(start.add(diff, "day"), "dd"));
  };
}

Note: that you will need to do this with every adapter you want to support to be in sync.

Extending behavior

It is possible also to add custom functionality to the adapter, in case the author of date-io doesn't want to add it. To do this create your custom interface that extends IUtils and inherit your adapters both from the base adapter and your custom interface.

typescript
// your-awesome-lib/adapters/CustomAdapter
import { IUtils } from "@date-io/core/IUtils";
import DateIODateFnsAdapter from "@date-io/date-fns";

interface CustomUtils extends  IUtils {
  getDayOfYear(day: TDate): number;
}

interface CustomDateFnsUtils extends DateIODateFnsAdapter implements CustomUtils {
  getDayOfYear(day: Date): number {
    return getDayOfYear(day);
  }
}

Build system

In some build systems (hello babel) it may be impossible to extend the methods because they are defined as class properties. While it should be standardized already there are still some issues with it. In this case you can use the following workaround:

…

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptdatedate-fnsdatetimedayjs

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言