Buy Me A Coffee 😇

The Best JavaScript Date Time Library

/ A 2KB immutable date-time library alternative to Moment.js with the same modern API. The minimalist library parses, validates, manipulates, and displays dates and times for modern browsers. It's the best library and you should use it, not it works in client and in node server.

Apr. 01, 2023. 10:03 AM

I am using momentjs before, and the library works really well. But After knowing Day.js · 2kB JavaScript date utility library, I have been using it for most of my projects, because I really like it small cause we don't like to have large JS files being included in our website that would affect the performance. I believe that a large library has an impact on our website, especially when loading it for the first time.

You can use `daysjs` in a node project by installing it using npm/yarn.

// npm
npm install dayjs

// yarn
yarn add dayjs

or You can use CDN

<!-- CDN example (jsDelivr) -->
<script src=""></script>

For more on how to use it on your project, you can check it here Installation · Day.js.

Here are the things why I love dayjs

  1. It has a ton of display options, you can display your date and time in a format you like.

    // current date in ISO8601, without fraction seconds e.g. '2020-04-02T08:02:17-05:00'
    dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]') 
    // 'YYYYescape 2019-01-25T00:00:00-02:00Z'
    dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
  2. Plugins: I like other functionality to be in a plugin that way, I can only grab something I need before using it. Like for example the UTC plugin. I really need this because if you're working on data that is being saved on the database. You want it to be in UTC format that way it's a lot easier to show time in different time zones.

    var utc = require('dayjs/plugin/utc')
    // default local time
    dayjs().format() //2019-03-06T17:11:55+08:00
    // UTC mode
    dayjs.utc().format() // 2019-03-06T09:11:55Z
    // convert local time to UTC time
    dayjs().utc().format() // 2019-03-06T09:11:55Z 
    // While in UTC mode, all display methods will display in UTC time instead of local time.
    // And all getters and setters will internally use the Date#getUTC* and Date#setUTC* methods instead of the Date#get* and Date#set* methods.
    dayjs.utc().isUTC() // true
    dayjs.utc().local().format() //2019-03-06T17:11:55+08:00
    dayjs.utc('2018-01-01', 'YYYY-MM-DD') // with CustomParseFormat plugin
  3. Manipulation: you can easily manipulate dates like adding 1 day to a date or minus 1 day to a date etc.

    dayjs('2019-01-25').add(1, 'day').subtract(1, 'year').year(2009).toString()
  4. Query: you can do some conditional that way you can check the date your trying to evaluate.

    // example
    // This requires the IsBetween plugin to work
    var isBetween = require('dayjs/plugin/isBetween')
    dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25')) 
    // default milliseconds
  5. Duration: This is a really important functionality because you can easily calculate the duration of a date to the current date.

    dayjs.duration(1, "minutes").humanize(); // a minute
    dayjs.duration(2, "minutes").humanize(); // 2 minutes
    dayjs.duration(24, "hours").humanize();  // a day
  6. Time zone: You can easily set time zones.

    // current time zone is 'Europe/Berlin' (offset +01:00)
    // Parsing"2013-11-18 11:55:20", "America/Toronto") // '2013-11-18T11:55:20-05:00'
    // Converting (from time zone 'Europe/Berlin'!)
    dayjs("2013-11-18 11:55:20").tz("America/Toronto") // '2013-11-18T05:55:20-05:00'

If you enjoy this article and would like to show your support, you can easily do so by buying me a coffee. Your contribution is greatly appreciated!

Jenuel Ganawed Buy me Coffee