Categories
Day.js

Manipulating Dates with Day.js — Get Different Date Values

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 Different Date Values

We can get different date values with the get method and the unit of the date part to get as the argument.

For instance, we write:

const dayjs = require("dayjs");
const result = [
  dayjs().get("year"),
  dayjs().get("month"),
  dayjs().get("date"),
  dayjs().get("hour"),
  dayjs().get("minute"),
  dayjs().get("second"),
  dayjs().get("millisecond")
];

console.log(result);

to get the year, month, date of the month, hour, minute, second, and milliseconds of a date with the get method and the 'year' , 'month' , 'date' , 'hour' , 'minute' , 'second' , and 'millisecond' respectively.

The month starts with 0 for January just like JavaScript dates.

There’re also shorthands for each argument string.

'y' is shorthand for 'year' .

'M' is shorthand for 'month' .

'D' is shorthand for 'date' .

'd' is shorthand for 'day' , which is the day of the week. It starts at 0 for Sunday, and 6 is Saturday.

'h' is shorthand for 'hour' .

'm' is shorthand for 'minute' .

's' is shorthand for 'second' .

And 'ms' is shorthand for 'millisecond' .

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 *