Common date/time operations without Moment.js

Common date/time operations without Moment.js

For date/time operations, Moment.js was once the recommended library, but now it's not being developed, and it's considered a legacy library. This article will help you replace that library with several alternatives, so you can keep doing modern development.

Multiple web applications require date and time operations. Moment.js once served as the recommended library for managing these processes. However, it is currently viewed as a legacy project. Moment.js maintenance and development have slowed down, so using other libraries like date-fns, Day.js, Luxon, and js-joda is advised.

These libraries offer more modern and effective date and time operations. We will also go over how to do similar actions using the built-in Date object in JavaScript. For formatting dates, conducting calculations, and turning strings into dates, the Date object has several methods and properties. This article will explore these functionalities and demonstrate how to carry out typical date and time operations without using Moment.js.

Brief Overview

Some common date and time operations that can be carried out without having to use the well-known Moment.js library utilizing JavaScript's built-in Date object will be covered in this article.

First, we’ll briefly mention the recommended alternatives to Moment.js, including date-fns, Day.js, Luxon, and js-joda.

We will then look into performing common date operations such as calculating the number of days between two dates, identifying if a year is a leap year, calculating a person's age based on their birthdate, and formatting dates to display them in a specific way. These operations will be performed using various methods and properties of the JavaScript Date object.

What is Moment.js

Moment.js is a popular JavaScript library for working with dates, times, and durations. It provides a useful JavaScript API for parsing, manipulating, and formatting dates and times, making it simple to use in various applications.

Moment.js offers developers a variety of customization options, such as time zone management, calendar compatibility, and more, to quickly create, change, and format dates and times. Several common dates and time formats are supported, along with relative dates and times such as "2 hours ago" or "in 5 days."

Moment.js has been a well-liked and dependable library for managing dates and times in JavaScript for many years due to its extensive use in web development.

But since Moment.js is now in maintenance mode, and its developers recommend using other libraries for new projects, we’ll be looking at how to perform some common date time operations without it.

Alternatives to Moment.js

Let’s briefly look at a few alternatives first.

date-fns

Date-fns is a JavaScript library for working with dates, times, and durations in JavaScript. It provides a simple API for parsing, manipulating, and formatting dates and times in JavaScript. Still, it is lightweight and modular, making it easier to use and customize for specific use cases.

It is based on pure functions and has a smaller file size than Moment.js, faster to load and more efficient to use, and has better support for modern JavaScript features. As a result, date-fns is a popular and reliable alternative to Moment.js and is more lightweight and flexible for date manipulation.

Here are a few reasons why you should consider date-fns:

  • Modular design with each function being a separate module

  • Lightweight file size for faster loading and improved performance

  • Built-in internationalization support (i18n) for formatting dates and times according to different locale conventions.

  • Comprehensive documentation, including TypeScript support, makes it easier to get started and use in your project.

Day.js

Day.js is another JavaScript library for working with dates, times, and durations. Like Moment.js and date-fns, it provides a simple API for parsing, manipulating, and formatting dates and times in JavaScript.

Day.js has several features and benefits, including:

  • Lightweight and Modern: Day.js has a very small file size, making it quick to load and use in your projects. It is also written in modern JavaScript and supports ES6 modules and other modern features.

  • Lightweight: Day.js has a very small file size, making it quick to load and use in your projects.

  • Chainable API: Day.js provides a chainable API that allows you to chain together multiple methods to create complex date manipulations.

  • Internationalization support: Day.js also has built-in support for internationalization (i18n)

  • Plugin system: Day.js has a plugin system that allows you to add additional functionality or customize the library to suit your needs.

Overall, Day.js is a lightweight and modern alternative to Moment.js. It also has comprehensive documentation that can help you learn more and use it in your project.

Luxon

Luxon is a JavaScript library for working with dates, times, and time zones from Moment. Like Moment.js and date-fns, it provides a simple API for parsing, manipulating, and formatting dates and times in JavaScript.

Here are some features and benefits of Luxon as an alternative to Moment.js:

  1. Immutable: Luxon is based on pure functions, meaning it has no global state or side effects. This makes it easier to use in modern web applications where immutability and functional programming are becoming increasingly important.

  2. Timezone support: Luxon has built-in support for working with time zones, including the ability to convert between time zones and to represent dates and times in a specific time zone.

  3. Fluent API: Luxon provides a fluent API that allows you to chain together multiple methods to create complex date manipulations.

  4. Human-friendly formatting: Luxon provides human-friendly formatting options for dates and times, making it easy to create user-friendly interfaces that display dates and times in a natural and intuitive way.

  5. Modern JavaScript support: Luxon is written in modern JavaScript and supports ES6 modules and other modern features.

Luxon has comprehensive documentation, including examples and API reference documentation, making it easy to get started and use in your project. You can get started with Luxon from their documentation.

js-joda

Lastly, we have js-joda, which like Moment.js, date-fns, and Luxon, provides a simple API for parsing, manipulating, and formatting dates and times in JavaScript. It is a standalone date and time implementation, unlike other date libraries like moment or date-utils.

Here are some key features and benefits of js-joda as an alternative to Moment.js:

  1. Immutable: js-joda is based on pure functions and immutable data structures, which means it has no global state or side effects. This makes it easier to use in modern web applications where immutability and functional programming are becoming increasingly important.

  2. Lightweight: js-joda has a lightweight footprint, only 43 kB minified and compressed, with no third-party dependencies.

  3. Comprehensive API: js-joda provides a comprehensive API for working with dates and times, including support for calendar systems, duration calculations, and time zones.

  4. ISO 8601 compliance: js-joda is designed to comply with the ISO 8601 standard for date and time representation, which is widely used in modern web applications.

  5. Modern JavaScript support: js-joda is written in modern JavaScript and supports ES6 modules and other modern features.

js-joda’s documentation is a great way to get started with the library.

JavaScript Date Object

So far, we’ve seen the alternatives to moment.js, and now we’ll look at the native date implementation for JavaScript, the Date Object which provides methods and properties for working with date and time. You can find all the date methods on the MDN Docs.

Let’s take a look at a few of these methods. First, we instantiate a date object to work with. To create a date instance, we use the new keyword to create a new Date object in JavaScript. Use the new keyword followed by the Date() constructor. Here's an example:

const now = new Date();
console.log(now) // Mon Mar 27 2023 14:20:19 GMT+0100 (West Africa Standard Time)

This will create a new Date object representing the current date and time. We can also create a Date object representing a specific date and time in the following ways:

Using the number of milliseconds since the ECMAScript epoch

The beginning of January 1, 1970, at midnight UTC is referred to as the ECMAScript epoch. We can get the desired date if we pass the number of milliseconds that have passed since the epoch.

const millisecondsSinceEpoch = 1679823119465;
const myDate = new Date(millisecondsSinceEpoch);
console.log(myDate) // Sun Mar 26 2023 10:31:59 GMT+0100 (West Africa Standard Time)

Using the Date Time string

We can also create a new Date object in JavaScript by passing a date/time string to the Date() constructor. The format of the date/time string must be recognized by the Date.parse() method, which can parse a wide variety of date/time formats, including ISO 8601 format.

const myDate = new Date("Sun Mar 26 2023 10:31:59");
console.log(myDate) // Sun Mar 26 2023 10:31:59 GMT+0100 (West Africa Standard Time)

Note that the format of the date/time string passed to the Date() constructor can vary depending on the locale and system settings of the device running the code.

Using the date and time components

We can create a new Date object in JavaScript by passing in the year, month (0-indexed), day, hour, minute, second, and millisecond values as separate arguments to the Date() constructor. For example:

const myDate = new Date(2023, 2, 27, 14, 10, 0, 0);
console.log(myDate) // Mon Mar 27 2023 15:08:00 GMT+0100 (West Africa Standard Time)

Now that we’ve seen how to create date instances, let's take a look at a few methods we can use on our date object.

Get Methods

Let's look at several methods we can use to retrieve the various components that the date object represents. Here’s an example of a few of the most commonly used methods:

const myDate = new Date("Mon Mar 27 2023 14:20:19");
const year = myDate.getFullYear(); // returns 2023
const month = myDate.getMonth(); // returns 2 (March is zero-indexed)
const dayOfMonth = myDate.getDate(); // returns 27
const dayOfWeek = myDate.getDay(); // returns 1 (Monday is day 1)
const hours = myDate.getHours(); // returns 14
const minutes = myDate.getMinutes(); // returns 20
const seconds = myDate.getSeconds(); // returns 19
const milliseconds = myDate.getMilliseconds(); // returns 0
const timeSinceEpoch = myDate.getTime(); // returns the number of milliseconds since the ECMAScript epoch (1679923219000 in this case)

Note that all of these methods return values based on the local time zone of the device running the code unless otherwise specified.

Set Methods

In addition to getting the different date and time components from our date object, we can also modify the date and time represented by our object. Let’s take a look at another example with a few of the most commonly used set methods:

const myDate = new Date("Mon Mar 27 2023 14:20:19");
myDate.setFullYear(2020); // changes the year to 2020
myDate.setMonth(0); // changes the month to January (zero-indexed)
myDate.setDate(10); // changes the day of the month to the 10th
myDate.setHours(12); // changes the hour to 12pm
myDate.setMinutes(30); // changes the minutes to 30
myDate.setSeconds(0); // changes the seconds to 0
myDate.setMilliseconds(0); // changes the milliseconds to 0
myDate.setTime(1578655800000); // changes the time to 12:30:00 PM on January 10, 2020 (as represented by the number of milliseconds since the epoch)

console.log(myDate) // Fri Jan 10 2020 12:30:00 GMT+0100 (West Africa Standard Time)

The 'Date' object is modified by all of these set methods, which return the milliseconds since the epoch that corresponds to the modified date and time.

Importance of time zones in date and time calculations

Because the same moment in time can be represented differently in several time zones, time zones play a significant role in date and time computations, and because the two geographic locations are in separate time zones, when it is noon in Lagos, Nigeria, which is UTC+01:00, it is 3 PM in the UAE (UTC+04:00).

As a result, it's critical to remember to account for the time zone while working with date and time calculations to prevent inaccurate outcomes.

Since JavaScript works with the UTC, there are a couple of UTC methods that are available on the date object like getUTCDate(), getUTCDay(), etc.

But in JavaScript, there is no time zone or string format stored in the Date object itself, so it's a bit tricky to work with time zones. However, we can use the getTimezoneOffset() method to get the difference in minutes between the local time zone and UTC (Coordinated Universal Time).

An operation we can perform with the Date object is to parse a string containing a time zone offset:

const dateString = "2023-03-27T00:00:00.000";
const GulfStandardTimeOffset = "+04:00";
const UTCDate = new Date(`${dateString}+00:00`);
const UAEDate = new Date(`${dateString}${GulfStandardTimeOffset}`)

UTCDate.toISOString() // '2023-03-27T00:00:00.000Z'
UAEDate.toISOString() // '2023-03-26T20:00:00.000Z'

We can see here that by setting the offset for each, we are able to obtain two standard date strings for two different time zones for the same time string.

First, we defined our date string, and the next line defines the time zone offset for the Gulf Standard Time. We then created a UTCDate Date instance with +00:00 UTC time zone offset and a Date instance for the same date and time but with the time zone offset set to the Gulf Standard Time.

By comparing the two strings returned by toISOString(), we can see that the second Date instance (UAEDate) has a different time zone offset than the first one (UTCDate).

We can also work with time zones using the ECMAScript Internationalization API, or Intl. The Intl API provides a standardized way to handle various internationalization and localization aspects, including working with dates, times, and time zones.

One of the key features of the Intl API is the DateTimeFormat object, which can be used to format date and time values according to a specific locale and time zone. To create a new DateTimeFormat object with a specific time zone, we can pass an options object with a timeZone property to the constructor:

const now = new Date("2023-03-27T00:00:00.000+00:00");
const options = {
  timeZone: "Africa/Lagos",
  timeStyle: "long",
  dateStyle: "full",
};
const formatter = new Intl.DateTimeFormat("en-US", options);
const formattedDate = formatter.format(now);
console.log(formattedDate); // 'Monday, March 27, 2023 at 1:00:00 AM GMT+1'

In this example, we first create a Date object representing a specific point in time, in this case, March 27, 2023, at midnight (00:00:00.000) in the UTC (Coordinated Universal Time) time zone.

We then create an options object with two properties: timeZone and timeStyle. The timeZone property is set to "Africa/Lagos", which is the time zone we want to use for formatting the date and time. The timeStyle property is set to "long", which means that we want to include the time component in the formatted string.

Next, we create a DateTimeFormat object using the Intl API, passing in the 'en-US' locale and the options object we just created. This creates a formatter to format our Date object according to the specified options.

Finally, we use the formatter's format() method to format our Date object and store the resulting string in the formattedDate variable. We then log this string to the console, which outputs 'Monday, March 27, 2023 at 1:00:00 AM GMT+1'.

This example demonstrates using the Intl API to format a Date object according to a specific time zone and format.


Session Replay for Developers

Uncover frustrations, understand bugs and fix slowdowns like never before with OpenReplay — an open-source session replay suite for developers. It can be self-hosted in minutes, giving you complete control over your customer data.

OpenReplay

Happy debugging! Try using OpenReplay today.


Common date operations with JavaScript Date object

Here, we look at some common date and time operations we can perform with the JavaScript Date object.

Calculating days between two dates

To calculate the number of days between two dates, we can subtract the earlier date from the later date and divide the result by the number of milliseconds per day (86400000).

Here's an example:

const date1 = new Date("2023-01-01");
const date2 = new Date("2023-02-15");
const msInDay = 1000 * 60 * 60 * 24 // 86400000
const diffInMs = Math.abs(date2 - date1);
const diffInDays = Math.ceil(diffInMs / msInDay);

console.log(diffInDays); // Output: 45

In this example, we have two date objects, date1 and date2. We use the Math.abs() method to ensure that we get the absolute value of the difference between the two dates, which would be a negative number if date1 is later than date2. We store this difference in milliseconds in the diffInMs variable.

Next, we calculate the number of milliseconds in a day by multiplying the number of milliseconds in a second (1000), the number of seconds in a minute (60), the number of minutes in an hour (60), and the number of hours in a day (24). This gives us the value of msInDay.

By dividing the difference in milliseconds by the total milliseconds in a single day, we can determine the number of milliseconds in a day. We use the Math.ceil() method to round up to the next whole integer if the difference in milliseconds is not an exact number of days.

Finally, we log the number of days to the console using console.log(). In this example, the output would be 45 since there are 45 days between January 1, 2023, and February 15, 2023.

We can take this further by determining the time that has elapsed since an event and present it in a custom format to the user.

Here's an example that formats a message based on the difference between two dates:

function getRelativeTime(date) {
  const currentDate = new Date();
  const diffInMilliseconds = currentDate - date;

  const seconds = Math.floor(diffInMilliseconds / 1000);
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);
  const months = Math.floor(days / 30);
  const years = Math.floor(months / 12);

  if (seconds < 60) {
    return "just now";
  } else if (minutes < 60) {
    return `${minutes} minute${minutes > 1 ? "s" : ""} ago`;
  } else if (hours < 24) {
    return `${hours} hour${hours > 1 ? "s" : ""} ago`;
  } else if (days < 30) {
    return `${days} day${days > 1 ? "s" : ""} ago`;
  } else if (months < 12) {
    return `${months} month${months > 1 ? "s" : ""} ago`;
  } else {
    return `${years} year${years > 1 ? "s" : ""} ago`;
  }
}

const uploadDate = new Date("2022-12-25T00:00:00.000+00:00");
const relativeTime = getRelativeTime(uploadDate);

console.log(`Uploaded ${relativeTime}`);
// Output: Uploaded 3 months ago

In this example, the getRelativeTime() function uses a date object as a parameter to determine how many seconds, minutes, hours, days, months, and years have passed since the specified date and the current date.

Then, the function returns a string representing the relative time between the two dates. The string is formatted to say "just now" if the difference is less than a minute, "x minute(s) ago" if the difference is less than an hour, "x hour(s) ago" if the difference is less than a day, "x day(s) ago" if the difference is less than a month, "x month(s) ago" if the difference is less than a year, and "x year(s) ago" if the difference is greater than or equal to a year.

Finally, the example creates a new date object for a specific upload date and passes it to the getRelativeTime() function. The resulting string is then logged to the console to show how many months ago the upload date was.

Getting the day of the week

To know the day of the week for a specific date in JavaScript, you can use the getDay() method of the Date object. This method returns an integer between 0 and 6, where 0 represents Sunday, 1 represents Monday, and so on.

We can now use this to get the day of the week by assigning each integer to a day of the week:

const myDate = new Date("2023-03-01");
const dayOfWeek = (day) => {
  const days = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
  ];
  return days[day];
};

dayOfWeek(myDate.getDay()); // 'Wednesday'

Here, we use the JavaScript Date object's getDay() method to obtain the day of the week in the form of an integer ranging from 0 (Sunday) to 6 (Saturday). To make it more human-readable, we can create an array of strings representing each day of the week, with Sunday being the first day of the week.

In this code, we create a new instance of the Date object using the date string '2023-03-01'. We then define a function dayOfWeek() that takes an integer representing the day of the week as an argument and returns the corresponding string from the days array.

Finally, we call the dayOfWeek() function, passing in the integer returned by getDay() on the myDate object. The function then returns the string 'Wednesday', which is the day of the week for the specified date.

We can also use toLocaleDateString() and Intl.DateTimeFormat to get the day of the week in a specific format. Here are some examples:

const date = new Date(); // get the current date

// Option 1: using toLocaleDateString
const dayOfWeek = date.toLocaleDateString("en-US", { weekday: "long" });
console.log(dayOfWeek); // e.g. "Wednesday"

// Option 2: using Intl.DateTimeFormat
const formatter = new Intl.DateTimeFormat("en-US", { weekday: "long" });
const dayOfWeek2 = formatter.format(date);
console.log(dayOfWeek2); // e.g. "Wednesday"

In these examples, toLocaleDateString() and Intl.DateTimeFormat are both used with the weekday option to get the full name of the day of the week. The en-US locale is used to format the date string in English.

Note that toLocaleDateString() and Intl.DateTimeFormat use the user's locale to format the date string, so the output may be different depending on the user's location and language preferences.

Identifying leap years

A leap year is any year that divides equally by 4, with the exception of years that divide evenly by 100. However, a year is a leap year if it can be divided by 400.

In JavaScript, we can use the getFullYear() method to get the year from a Date object and then check if it is a leap year using the above conditions. Here's an example:

const getLeapYear = (year) =>
  (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
const today = new Date();
const year = today.getFullYear();
const isLeapYear = getLeapYear(year);

console.log(`Is ${year} a leap year? ${isLeapYear}`); // Is 2023 a leap year? false

In this code snippet, we have created a function called getLeapYear that takes in a year and returns true if it is a leap year. We then use the getFullYear() method to get the current year from the user's system and store it in a variable called year. Next, we call our getLeapYear function, passing in the current year, and store the result in a variable called isLeapYear. Finally, we use console.log() to output a message to the console, stating whether the current year is a leap year or not. So, when we run this code, we will see a message in the console telling us whether the current year (which we obtained using getFullYear()) is a leap year or not (which we determined using our getLeapYear() function).

Calculating age based on birthdate

There are a number of ways to calculate a person’s age from their birthdate using JavaScript. We’ll be calculating based on the difference between their birthdate and the current date and then converting the difference into years.

const getAge = (birthdate) => {
  const now = new Date();
  const birthDate = new Date(birthdate);
  let age = now.getFullYear() - birthDate.getFullYear();
  const monthDiff = now.getMonth() - birthDate.getMonth();
  if (
    monthDiff < 0 ||
    (monthDiff === 0 && now.getDate() < birthDate.getDate())
  ) {
    age--;
  }
  return age;
};
const birthdate = "2004-02-28";
const age = getAge(birthdate);

console.log(`You are ${age} years old`); // You are 19 years old

Here, we have a function called getAge, which takes a birthdate as an argument. Inside the function, we create two Date objects: one for the current date, now, and the other for the birthdate.

Next, we calculate the age by subtracting the birth year from the current year.

Then, we check if the birth month is greater than the current month. If it is, then the person has not yet had their birthday this year, so we decrement the age.

Similarly, if the birth month is the same as the current month, but the birth date is greater than the current date, then the person has not yet had their birthday this year, so we decrement the age.

Then, we return the age.

After that, we define a birthdate as a string of "2004-02-28". We then call the getAge function with this birthdate as an argument and assign the result to the variable age.

Finally, we log a message to the console that tells us how old we are using the age variable.

Importance of considering leap years in age calculations

A year is about 365¼ or 365.25 days. In a calendar year, it is 366 or 365 days depending on whether the year is a leap year or not. It’s important to consider this when trying to calculate age because it leads to some issues when trying to calculate age using the number of days from a birthdate.

If we’re calculating using the number of days, we have to account for leap years due to the extra day in a leap year. This might end up throwing our calculations off by one year.

To do this, we need to check if the year of the birthdate is a leap year. We can do this by checking if the year is divisible by 4 but not divisible by 100, or if it is divisible by 400. If it is a leap year, we add an extra day to the age calculation. This ensures that our age calculation is accurate and accounts for the extra day in a leap year.

The example shown previously is still a great way to calculate age as it does not rely on the number of days in a year and so is not prone to errors due to leap years.

Date formatting

The JavaScript Date object provides us with multiple ways we can format our date into a human-readable string representation. There are several built-in methods for formatting dates. The most commonly used method is toLocaleDateString(), which formats a date according to the locale of the user. For example:

const date = new Date();
const formattedDate = date.toLocaleDateString();

console.log(formattedDate); // Output: "3/28/2023" (depending on the user's locale)

The toLocaleDateString() method can also take options as an argument to customize the formatting. For example:

const date = new Date();
const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
};
const formattedDate = date.toLocaleDateString("en-US", options);
console.log(formattedDate); // Output: "Friday, February 17, 2023"

In this example, we're using the options object to specify that we want the date to be formatted with the full name of the weekday, month, and year.

Other built-in methods for formatting dates include toISOString(), toUTCString(), and toDateString()

toISOString()

The toISOString() method will return a string representing the date in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ). Here's an example:

const now = new Date();
console.log(now.toISOString()); // 2023-03-28T05:59:09.293Z

toUTCString()

The toUTCString() method returns a string representing the date in UTC format (e.g. Fri, 17 Feb 2023 05:34:29 GMT). Here's an example:

const now = new Date();
console.log(now.toUTCString()); // Tue, 28 Mar 2023 06:01:17 GMT

toDateString()

The toDateString() method returns a string representing the date portion of the date object in a human-readable format (e.g., Fri Feb 17 2023). Here's an example:

const now = new Date();
console.log(now.toDateString()); // Tue Mar 28 2023

Note that the output format of these methods may differ based on your local timezone or browser settings.

Using the Intl.DateTimeFormat

The Intl.DateTimeFormat object is a built-in JavaScript object that provides a way to format dates and times according to the user's locale.

Here's an example of how to use Intl.DateTimeFormat:

const date = new Date();
const options = {
  timeZone: "Africa/Lagos",
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
  hour: "numeric",
  minute: "numeric",
};
const formatter = new Intl.DateTimeFormat("en-US", options);

const formattedDate = formatter.format(date);

console.log(formattedDate); // Tuesday, March 28, 2023 at 7:34 AM

This code here formats the current date and time using the Intl.DateTimeFormat object.

First, a new Date object is created with the current date and time. Then, an options object is defined with various options to format the date and time. The timeZone option is set to 'Africa/Lagos' to specify the time zone for formatting the date and time.

The other options specified include weekday, year, month, day, hour, and minute. These are just some of the options available. You can find more in the MDN Docs. These options control the output format for the date and time.

The Intl.DateTimeFormat constructor is used to create a formatter object with the options and the locale specified as 'en-US'.

Finally, the format method of the formatter object is called with the date object as an argument to produce a formatted date and time string. This formatted string is then printed to the console. The output displays the weekday, month, day, year, and time in the specified time zone.

Conclusion

In conclusion, the JavaScript Date object is a powerful tool for working with date and time in JavaScript without relying on external libraries such as Moment.js. We have covered various ways to create and manipulate Date objects, including using the number of milliseconds since the epoch, the Date Time string, and date and time components. We have also explored various get and set methods available on the Date object, and the importance of considering time zones in date and time calculations.

Additionally, we have covered common date operations with the JavaScript Date object, such as calculating the number of days between two dates, identifying leap years, and calculating age based on birthdate, and the importance of considering leap years in age calculations. Finally, we have looked at various methods for formatting dates, including toISOString(), toUTCString(), and toDateString(), as well as using the Intl.DateTimeFormat.

By understanding these concepts and techniques, you can easily work with dates and times in JavaScript and create sophisticated applications without the need for external libraries.