JavaScript Date prototype

Posted by

Introduction to the JavaScript Date Prototype

The JavaScript Date prototype is a powerful tool that allows you to manipulate dates and times in a variety of ways. It’s important to understand how the Date prototype works in order to properly use it in your JavaScript code. In this tutorial, we’ll go over the basics of the Date prototype and how it can be used to work with dates and times.

Constructing a Date Object

The first step in using the Date prototype is to create a Date object. This can be done by using the Date constructor, which takes a single parameter – the date and time you want to create an object for. The parameter can either be in the form of a string, a timestamp, or a set of numerical values. Here’s an example of using the Date constructor with a string:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let date = new Date("January 1, 2021");

[/dm_code_snippet]

The Date constructor can also be used with a timestamp. The timestamp is a number of milliseconds since the beginning of the epoch. Here’s an example of creating a Date object with a timestamp:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let timestamp = 1609459200000;
let date = new Date(timestamp);

[/dm_code_snippet]

Finally, you can create a Date object with a set of numerical values. These values represent the year, month, day, hour, minute, and second of the date and time you want to create. Here’s an example of creating a Date object with numerical values:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let date = new Date(2021, 0, 1, 0, 0, 0);

[/dm_code_snippet]

Accessing Date Components

Once you’ve created a Date object, you can access the individual components of the date and time. There are several methods available that allow you to access the year, month, day, hour, minute, and second of the date. Here are some examples of accessing the components of a Date object:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let date = new Date("January 1, 2021");
// Get the year
let year = date.getFullYear(); // 2021
// Get the month
let month = date.getMonth(); // 0 (January)
// Get the day
let day = date.getDate(); // 1
// Get the hour
let hour = date.getHours(); // 0
// Get the minute
let minute = date.getMinutes(); // 0
// Get the second
let second = date.getSeconds(); // 0

[/dm_code_snippet]

Formatting Dates

The Date prototype also provides a number of methods for formatting dates and times to specific formats. The most commonly used method is the toLocaleString() method. This method takes an optional parameter, which is an object containing options for formatting the date. Here’s an example of using the toLocaleString() method to format a date:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let date = new Date("January 1, 2021");
let formattedDate = date.toLocaleString({
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric',
});
// Output: Friday, January 1, 2021

[/dm_code_snippet]

Calculating Dates

The Date prototype also provides methods for calculating dates and times. These methods allow you to add or subtract days, months, or years from a Date object. Here are some examples of using these methods:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let date = new Date("January 1, 2021");
// Add one year
let newDate = date.setFullYear(date.getFullYear() + 1);
// Subtract one month
let newDate = date.setMonth(date.getMonth() - 1);
// Add one day
let newDate = date.setDate(date.getDate() + 1);

[/dm_code_snippet]

Conclusion

In this tutorial, we’ve gone over the basics of the JavaScript Date prototype. We’ve seen how to create Date objects, access their components, and use them for calculations and formatting. With the knowledge you’ve gained from this tutorial, you should now have a better understanding of how to use the Date prototype to work with dates and times in your JavaScript code.