Language: EN

javascript-tipo-date

The Date Type in JavaScript

In JavaScript, the type Date is a basic type that allows us to handle dates and times and perform operations with them.

This object represents a moment in time, expressed as the number of milliseconds elapsed since January 1, 1970 at 00:00:00 UTC (also known as the “Unix epoch”)

The Date type in JavaScript is not a primitive type, like string or number, but an object.

Creating a Date object

To create a Date object, you can use the Date() constructor, which accepts different types of parameters:

If we use new Date() without parameters, it creates an object with the current date and time.

const currentDate = new Date();
console.log(currentDate); // Displays the current date and time

We can also specify a date in the format YYYY, MM, DD, HH, MM, SS.

const specificDate = new Date(2023, 8, 20); // September 20, 2023

You can also create a Date object from a string.

const dateFromString = new Date("2023-09-20T10:20:30Z");

Finally, you can also create a Date object from a number representing milliseconds since the Unix epoch.

const dateFromMilliseconds = new Date(1672531199000); // January 1, 2023

These last two may not be very readable, but they are very useful when working with previously stored dates (for example, in a database)

Obtaining date components

Once a Date object is created, we can get its individual components (such as year, month, day, hour, minute, and second):

const date = new Date(2023, 8, 20, 15, 30, 0);
console.log(date.getFullYear()); // 2023
console.log(date.getMonth()); // 8 (September, months start at 0)
console.log(date.getDate()); // 20
console.log(date.getHours()); // 15
console.log(date.getMinutes()); // 30
console.log(date.getSeconds()); // 0

Remember that collections in JavaScript start at 0. So the months go from 0 (January) to 11 (December)

Date Operations

We can perform arithmetic operations with Date objects, such as calculating differences and additions.

Difference between dates

To calculate the difference between two dates, we can subtract them to obtain the difference in milliseconds.

const date1 = new Date('2024-08-14');
const date2 = new Date('2024-08-21');

const difference = date2 - date1; // Difference in milliseconds

const differenceInDays = difference / (1000 * 60 * 60 * 24);

console.log(differenceInDays); // 7 days

Adding or subtracting time

To add or subtract time from a date, you can use the corresponding set methods.

const date = new Date();
date.setDate(date.getDate() + 10); // Adds 10 days
console.log(date);

date.setMonth(date.getMonth() - 1); // Subtracts 1 month
console.log(date);

Comparing dates

Date objects can be compared directly using comparison operators (internally, they are converted to milliseconds when evaluated),

const date1 = new Date(2023, 8, 20);
const date2 = new Date(2023, 8, 21);

console.log(date1 < date2); // true
console.log(date1 > date2); // false

Methods to manipulate Date

JavaScript provides many methods for working with Date.

Time zones

Dates and times in JavaScript are affected by time zones (which can be a real headache in many circumstances).

Generally, the Date object uses the time zone of the execution environment. To work with UTC (Coordinated Universal Time), use those that contain UTC.

const date = new Date();
console.log(date.toLocaleString()); // Date and time in local time zone
console.log(date.toUTCString());    // Date and time in UTC

UTC is the global time standard used as a reference for synchronizing clocks worldwide. It does not adjust to time zones and is independent of the Earth’s rotation.