Language: EN

javascript-tipo-date

The Date Type in JavaScript

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

This object represents a specific moment in time, expressed as the number of milliseconds that have 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 rather 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); // Shows the current date and time

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

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

The last two methods 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 obtain 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 months range from 0 (January) to 11 (December)

Operations with Dates

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 get 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 to work 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 the methods 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 around the world. It does not adjust to time zones and is independent of the Earth’s rotation.