javascript-tipo-date

The Date Type in JavaScript

  • 3 min

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 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, an object with the current date and time is created.

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

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
Copied!

A Date object can also be created from a text string.

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

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

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

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

Date Operations

We can perform arithmetic operations with Date objects, for example, difference calculations and additions.

Date Difference

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
Copied!

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);
Copied!

Date Comparison

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
Copied!

Methods for Manipulating 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 runtime environment. To work with UTC (Coordinated Universal Time), use the ones 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
Copied!

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