Day.js is a JavaScript library that lets us manipulate dates in our apps.
In this article, we’ll look at how to use Day.js to manipulate dates in our JavaScript apps.
Set the Quarter of the Year of a Date
To set the quarter of the year in a Day.js date we can use the quarter
method available with the quarterOfYear
plugin:
const dayjs = require("dayjs");
const quarterOfYear = require("dayjs/plugin/quarterOfYear");
dayjs.extend(quarterOfYear);
const result = dayjs().quarter(1);
console.log(result);
We import the quarterOfYear
plugin with:
const quarterOfYear = require("dayjs/plugin/quarterOfYear");
And we set the quarter of the year to the first quarter by calling quarter
with 1.
Set the Year of a Date
To set the year of a date in a Day.js date we can use the year
method available with the year
plugin:
const dayjs = require("dayjs");
const result = dayjs().year(2020);
console.log(result);
We set the year of the date to 2020 by calling year
with 2020.
Maximum Between Multiple Dates
To get the maximum of multiple dates, we can use the max
method available with the minMax
plugin:
const dayjs = require("dayjs");
const minMax = require("dayjs/plugin/minMax");
dayjs.extend(minMax);
const result = dayjs.max(
dayjs("2020-01-01"),
dayjs("2018-01-01"),
dayjs("2019-01-01")
);
console.log(result);
We import the minMax
plugin with:
const minMax = require("dayjs/plugin/minMax");
And we get the max date from the dates we passed into the max
method.
Conclusion
Day.js is a JavaScript library that lets us manipulate dates in our apps.