For date and time processing, there are commonly libraries such as moment.js Luxon, etc. However, these libraries are a little big in personal opinion, and ordinary people often only use individual functions, which is a little waste. Of course, you can also extract the desired modules, but it is a little tedious. I usually like to encapsulate my own time handling methods for this kind of situation, but when I found out that there was a 2K date-time JS library dayJS that grew to over 8500STAR shortly after it appeared, I thought it was worth checking to see if it clicked.

As described on Github, DayJS has the following features:

  • 🕒 Familiar Moment.js API & patterns
  • 💪 Immutable
  • 🔥 Chainable
  • 📦 2 KB mini library
  • 👫 All Browsers support are tempting just with immutable objects and chained calls.

It’s also easy to install and use:

    npm install dayjs --save

    var dayjs = require('dayjs');
    dayjs().format();
Copy the code

Then experience its power from this piece of code:

dayjs()
    .endOf('month')
    .add(1, 'day')
    .set('year', 2018)
    .format('YYYY-MM-DD HH:mm:ss');
Copy the code

Chain processing: Get the last day of the current month -> plus one day -> minus one year from now. The function is easy to use, and this kind of chain way quite likes.

The API looks the same as moment. It feels like we can replace the old project’s moment.js with day.js, which is a lot smaller.

There are many similar functions that are useful to use, but more details can be found on Github.