![]() |
VOOZH | about |
dayjs is a lightweight JavaScript library that simplifies date manipulation and formatting. It is often considered a modern alternative to the popular Moment.js library due to its small size and similar API.
These are the following topics that we are going to discuss:
Table of Content
dayjs is a JavaScript library that provides an easy-to-use API for working with dates and times. It offers functionality similar to Moment.js but is much smaller in size (just 2KB), making it ideal for projects where you need to minimize the size of your bundle. Some of the main features of dayjs include:
mkdir dayjs-example
cd dayjs-example
npm init -ynpm install dayjs"dependencies": {
"dayjs": "^1.11.13"
}
dayjs allows you to format dates in a variety of ways. The format() method can be used to convert a date object into a string with a specified format.
Example: In this example we will be formatting dates
Output:
2024-10-08
Tuesday, October 8, 2024 10:25 AM
You can also parse dates from strings using dayjs. It supports ISO 8601 date strings and many other formats.
Example: In this example we will parse dates
Output:
2024-10-08T00:00:00-07:00dayjs allows you to manipulate dates by adding or subtracting time.
Example: Adding time to a date
Output:
2024-10-15Comparing dates in dayjs is simple using methods like isBefore(), isAfter(), and isSame().
Example: In this example we will be comparing the dates
Output:
true
false
true
dayjs is a fantastic, lightweight JavaScript library for managing dates and times in your projects. With a minimal footprint and an intuitive API, it offers a simple yet powerful solution for developers who need to handle dates efficiently. Whether you're formatting dates, manipulating time, or adding timezone support through plugins, dayjs provides all the essential features you need.