Categories
Day.js

Manipulating Dates with Day.js — Get Seconds, Minutes, Hours, Month, and Week 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.

Getting Seconds of a Date

We can get the value with the valueOf method or methods for getting the parts of the time.

To get the number of seconds in a Day.js date we can use the second method:

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

This is the same as the native getSeconds method.

Getting Minutes of a Date

To get the number of minutes in a Day.js date we can use the minute method:

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

Getting Hours of a Date

To get the number of hours in a Day.js date we can use the hour method:

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

Getting Month of a Date

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

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

Getting Day of the Week of a Date

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

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

Getting Locale-Aware Day of the Week of a Date

To get the locale-aware day of the week in a Day.js date we can use the weekday method:

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

Getting ISO Day of the Week of a Date

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

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

We import the isoWeek plugin with:

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

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 *