How to use quarters in Day.js
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
Search for a command to run...
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
No comments yet. Be the first to comment.
In this series, I'll check how various date manipulation libraries perform in the build - especially in regards to output size.
In this article, I'll summarize Day.js & date-fns comparison, that concludes this series. Introduction Based on what I've learned in the first round of articles, Day.js & date-fns look the most interesting. date-fns In the first test, where I used o...
I’ve been working in programming for the last 16 years. Let’s see what changes I see in our industry that are enabled by generative AI. Rapid prototyping The last few months, I’ve been working on a set of WordPress plugins, related to event organizat...
Writing unit tests takes time and effort. Nonetheless, many teams insist on writing them anyway—that’s because of the benefits they bring to a project. Those benefits are mainly the following: fast feedback—unit tests speed up each iteration of twea...

Pure functions are the perfect case for unit testing. For a given input, we always expect the same output—there is no internal state involved. Let’s take a look at a few examples and some simple tests that check if the methods work as expected. Jasmi...

Let’s say you have a job interview in a few days. How should you prepare for it so that you can make an informed decision about joining the company, as well as make sure that your interests are taken care of? Prepare your questions An interview is a ...

Creating example projects is a common way of showing your skills to potential employers. Let’s take a look at what’s important to keep in mind when building personal projects with an eye toward impressing prospective employers. Simplicity Building ap...

This article will show how to work around a small gotcha in Day.js and show the library's code for the second round of the build size benchmark.
As I explained in the second article about date-fns, comparing the use of only one method skews our benchmark. Especially as one library is full-on tree-shaking and the other doesn't support it. So I developed a bit more complex example, and the article linked before shows it in date-fns.
My first try was straightforward, same code as I would use with Moment.js:
import dayjs from "dayjs";
const today = dayjs(),
quarterStart = dayjs().startOf("quarter"),
diffDays = today.diff(quarterStart, "days");
console.log("Yesterday was", dayjs().subtract(1, "day").toDate());
console.log(
"The quarter started on",
quarterStart.format("YYYY-MM-DD"),
"and today, it is",
diffDays,
"days since then"
);
To my surprise, the log I've got is:
The quarter started on 2021-08-06, and today, it is 0 days since then.
The quarter didn't start on the 6th of august.
Via GitHub issue, I found in the documentation QuarterOfYear plugin:
import quarterOfYear from 'dayjs/plugin/quarterOfYear'
dayjs.extend(quarterOfYear)
dayjs('2010-04-01').quarter() // 2
dayjs('2010-04-01').quarter(2)
So in my example, I just needed to add 2 lines to have everything work as expected:
import dayjs from "dayjs";
+import quarterOfYear from "dayjs/plugin/quarterOfYear";
+
+dayjs.extend(quarterOfYear);
const today = dayjs(),
quarterStart = dayjs().startOf("quarter"),
For sure, the first encounter with plugins in this library wasn't smooth. It looked more like a library bug than an incomplete import. I wonder if throwing an error or logging something in the consoles would be a better dev experience. Maybe now, knowing Day.js is not such a monolith, I'll expect some plugins import?
The final code builds:
The final code is available here, and here the first try.
In this article, we have walked through an update to Day.js code for the second round of the build size benchmark. Besides, we discussed the quarter plugin gotcha the library has.