Categories
Day.js

Manipulating Dates with Day.js — Get Year Values of a Date

Spread the love

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.

Get the Date of the Year

To get the day of the year in a Day.js date we can use the dayOfYear method available in the dayOfYear plugin:

const dayjs = require("dayjs");
const dayOfYear = require("dayjs/plugin/dayOfYear");
dayjs.extend(dayOfYear);
const result = dayjs().dayOfYear();
console.log(result);

We import the dayOfYear plugin with:

const dayOfYear = require("dayjs/plugin/dayOfYear");

Get the Week of the Year of a Date

To get the week of the year in a Day.js date we can use the week method available in the weekOfYear plugin:

const dayjs = require("dayjs");
const weekOfYear = require("dayjs/plugin/weekOfYear");
dayjs.extend(weekOfYear);
const result = dayjs().week();
console.log(result);

We import the weekOfYear plugin with:

const weekOfYear = require("dayjs/plugin/weekOfYear");

Get the ISO Week of the Year of a Date

To get the ISO week of the year in a Day.js date we can use the isoWeek method available in the isoWeek plugin:

const dayjs = require("dayjs");
const isoWeek = require("dayjs/plugin/isoWeek");
dayjs.extend(isoWeek);
const result = dayjs().isoWeek();
console.log(result);

We import the isoWeek plugin with:

const isoWeek = require("dayjs/plugin/isoWeek");

Get the Month of a Date

To get the ISO week of the year in a Day.js date we can use the month method:

const dayjs = require("dayjs");
const result = dayjs().month();
console.log(result);

Get the Quarter of a Date

To get the ISO week 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();
console.log(result);

We import the quarterOfYear plugin with:

const quarterOfYear = require("dayjs/plugin/quarterOfYear");

Get the Year of a Date

To get the ISO week of the year in a Day.js date we can use the year method:

const dayjs = require("dayjs");
const result = dayjs().year();
console.log(result);

Get the Number of Weeks of the Year

To get the number of weeks of the year in a Day.js date we can use the isoWeeksInYear method available with the isoWeeksInYear and isLeapYear plugins:

const dayjs = require("dayjs");
const isoWeeksInYear = require("dayjs/plugin/isoWeeksInYear");
const isLeapYear = require("dayjs/plugin/isLeapYear");
dayjs.extend(isoWeeksInYear);
dayjs.extend(isLeapYear);

const result = dayjs().isoWeeksInYear();
console.log(result);

We import the plugins with:

const isoWeeksInYear = require("dayjs/plugin/isoWeeksInYear");
const isLeapYear = require("dayjs/plugin/isLeapYear");

Conclusion

Day.js is a JavaScript library that lets us manipulate dates in our apps.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *