Imagine you're working on a web application and you need to display the current date and time in a specific format. Maybe you want to show it in a dropdown menu, or perhaps you want to sort data based on the date it was created. In these scenarios, having a solid understanding of date and time formats is crucial. This is where Day.js, a lightweight yet powerful JavaScript library, shines. It simplifies date manipulation and offers a wide range of date and time formats. Let's explore how you can format dates using Day.js.

Day.js provides a comprehensive set of modifiable packages like advanced formatting, relative time, internationalization, and plugins for additional functionality. However, for now, let's focus on the core feature: the 'format' functionality. This tool allows you to format dates in a wide variety of ways, tailoring them to your specific needs.

Basic Date and Time Formatting
Before we dive into the specifics, let's take a look at the basic structure of date and time formatting in Day.js. It follows the `YYYY-MM-DDTHH:mm:ss` format, which you might already be familiar with if you've worked with other date manipulation libraries.

However, Day.js makes it incredibly easy to manipulate this format. Here's an example:
```javascript let testDate = dayjs('2022-01-01 00:00:00'); console.log(testDate.format()); // Outputs: "2022-01-01T00:00:00" ```
Custom Formatting

Now, let's say you want to display the date in a different format, like 'January 1, 2022'. Day.js allows you to easily customize the output:
You canβt use 'M' or 'mm' in the format string to get the result 'Jan' or '01' in this specific case. Instead, you need to use the 'Do' unit which can give you the month's full name:
```javascript console.log(testDate.format('MMMM DD, YYYY')); // Outputs: "January 01, 2022" ```
Day.js Token Reference

Day.js comes with a comprehensive list of formatting tokens that can be used to customize the date and time display. For instance, 'd' represents the day of the month (1-31), 'ddd' gives the full weekday name, and 'y' gives the two-digit year:
```javascript console.log(testDate.format('dddd, M DD, YYYY')); // Outputs: "Saturday, 1 01, 2022" ```
Advanced Formatting: Timespans and Duration
Day.js also has the ability to manage timespans and duration. This helps in calculating differences between two dates, or displaying the time since an event happened:

Calculating Difference
Let's say you want to find the difference between two dates:







```javascript let startDate = dayjs('2022-01-01 00:00:00'); let endDate = dayjs('2022-01-10 12:36:42'); console.log(endDate.diff(startDate, 'hours')); // Outputs: 260.61583333333335 ```
Duration Formatting
You can also display this difference in human-readable format:
```javascript console.log(dayjs.duration(260.61583333333335, 'hours').format('hh:mm:ss')); // Outputs: "10:13:23" ```
With this robust functionality in tow, Day.js allows developers to easily adapt dates and times to the specific requirements of their projects. Whether you're dealing with simple date display or complex timespan calculations, Day.js has you covered, all while keeping your bundle size small and your development experience fast and efficient.
But the journey doesn't stop here β Day.js' immense plugin support means this tool can adapt to an endless list of use cases, making it an incredibly flexible choice for your date and time management needs. Start exploring its wide range of plugins and options today, and step into the future of date and time manipulation with Day.js!