Categories
Day.js

Manipulating Dates with Day.js —Formatting Dates

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.

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 year
  • YYYY — Four-digit year
  • M — month, beginning at 1
  • MM — month, 2-digits
  • MMM — abbreviated month name
  • MMMM — full month name
  • D— day of the month
  • DD — day of the month, 2-digits
  • d — day of the week, with Sunday as 0
  • dd — min name of the day of the week
  • ddd — short name of the day of the week
  • dddd — name of the day of the week
  • H — hour
  • HH — hour, 2-digits
  • h — hour, 12-hour clock
  • hh — hour, 12-hour clock, 2-digits
  • m — minute
  • mm — minute, 2-digits
  • s — second
  • ss — second, 2-digits
  • SSS — millisecond, 3-digits
  • Z — offset from UTC, ±HH:mm
  • ZZ — offset from UTC, ±HHmm
  • A — AM PM
  • a — am pm

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 *