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 Date of the Month of a Date
To set the date of the month in a Day.js date we can use the date
method:
const dayjs = require("dayjs");
const result = dayjs().date(1);
console.log(result);
We set the date of the month of the date to 1 with 1 as the argument of date
.
Set the Day of the Week of a Date
To set the day of the week in a Day.js date we can use the day
method:
const dayjs = require("dayjs");
const result = dayjs().day(1);
console.log(result);
We set the day of the week of the date to Monday with 1 as the argument of day
.
Day of the week ranges from 0 for Sunday to 6 for Saturday.
Set the Locale-Aware Day of the Week of a Date
To set the locale-aware day of the week in a Day.js date we can use the weekday
method available with the weekday
plugin:
const dayjs = require("dayjs");
const weekday = require("dayjs/plugin/weekday");
dayjs.extend(weekday);
const result = dayjs().weekday(-1);
console.log(result);
We import the weekday
plugin with:
const weekday = require("dayjs/plugin/weekday");
And we set the locale-aware day of the week of the date to last Monday with -1 as the argument of weekday
.
Conclusion
Day.js is a JavaScript library that lets us manipulate dates in our apps.