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.
Formatting Dates
We can format Day.js dates into a date string with the format
method.
For instance, we can write:
const dayjs = require("dayjs");
const result = dayjs().format();
console.log(result);
to return the ISO-8601 format date string of the current date-time.
We can specify how the date is formatted with a string as the argument of format
:
const dayjs = require("dayjs");
const result = dayjs().format("DD/MM/YYYY");
console.log(result);
The following format strings can be added to format dates:
YY
— Two-digit yearYYYY
— Four-digit yearM
— month, beginning at 1MM
— month, 2-digitsMMM
— abbreviated month nameMMMM
— full month nameD
— day of the monthDD
— day of the month, 2-digitsd
— day of the week, with Sunday as 0dd
— min name of the day of the weekddd
— short name of the day of the weekdddd
— name of the day of the weekH
— hourHH
— hour, 2-digitsh
— hour, 12-hour clockhh
— hour, 12-hour clock, 2-digitsm
— minutemm
— minute, 2-digitss
— secondss
— second, 2-digitsSSS
— millisecond, 3-digitsZ
— offset from UTC, ±HH:mmZZ
— offset from UTC, ±HHmmA
— AM PMa
— am pm
Conclusion
Day.js is a JavaScript library that lets us manipulate dates in our apps.