1. javascript
  2. /basics
  3. /date-methods

JavaScript Date Methods Explained

Introduction

Dates can be a tricky concept to handle in programming. JavaScript provides a built-in Date object to handle dates and times, with its helpful methods and properties.

According to ECMAScript, the standardized format for representing dates in JavaScript is based on the ISO 8601 Extended format. The time is counted in milliseconds, starting from January 1st, 1970, 00:00:00 UTC. This means that every date and time can be represented as a numerical value in JavaScript.

The Date Object Syntax

The Date object can be created using the new Date() constructor, which returns a new Date object representing the current date and time:

let today = new Date();
console.log(today);

Also, we can the Date() function, which returns a string representation of the current date and time:

let today = Date();
console.log(today);

Listed Variety of Date Methods

Date methods are functions that can be used on a Date object to perform various operations. There are many different methods available, but in modern web development, it's preferable to use libraries instead of built-in methods. Although, for some basic requirements it may be simpler to use the built-in methods listed below.

MethodDescription
getDate()Returns the day of the month (from 1-31) for the specified date according to local time
getDay()Returns the day of the week (from 0-6) for the specified date according to local time
getFullYear()Returns the four-digit year (yyyy) of the specified date according to local time
getHours()Returns the hour (from 0-23) of the specified date according to local time
getMilliseconds()Returns the milliseconds (from 0-999) of the specified date according to local time
getMinutes()Returns the minutes (from 0-59) of the specified date according to local time
getMonth()Returns the month (from 0-11) of the specified date according to local time
getSeconds()Returns the seconds (from 0-59) of the specified date according to local time
getTime()Returns the number of milliseconds since January 1, 1970, 00:00:00 UTC, for the specified date
getTimezoneOffset()Returns the time-zone offset in minutes for the current locale
setDate(date)Sets the day of the month for a specified date according to local time
setFullYear(year[, month, date])Sets the full year (yyyy) for a specified date according to local time
setHours(hours[, minutes, seconds, milliseconds])Sets the hour for a specified date according to local time
setMilliseconds(milliseconds)Sets the milliseconds for a specified date according to local time
setMonth(month[, date])Sets the month for a specified date according to local time
setSeconds(seconds[, milliseconds])Sets the seconds for a specified date according to local time
setTime(time)Sets the date and time by adding or subtracting a specified
toDateString()Returns the "date" portion of the Date as a human-readable string
toISOString(Returns the date as a string, using the ISO standard
toJSON()Returns the date as a string, formatted as a JSON date
toLocaleDateString()Returns the date portion of a Date object as a string, using locale conventions
toLocaleString()Converts a Date object to a string, using locale conventions
toLocaleTimeString()Returns the time portion of a Date object as a string, using locale conventions
valueOf()Returns the primitive value of a date object
toLocaleTimeString()Returns a string representation of the time, in the format appropriate for the host environment's current locale
toString()Returns a string representation of the date
toTimeString()Returns the time portion of the Date as a human-readable string
toUTCString()Converts a date to a string, using the universal time convention

Comparing Dates

We can compare dates by subtracting one date object from another and checking the result. The result will be the difference in milliseconds between the two dates, which can then be divided by the number of milliseconds in a day, hour, minute, or second to get the difference in the desired unit.

The example below illustrates just this:

let date1 = new Date("2022-06-01");
let date2 = new Date("2022-07-01");
if (date1 > date2) {
    console.log("Date1 is after Date2");
} else if (date1 < date2) {
    console.log("Date1 is before Date2");
} else {
    console.log("Date1 is the same as Date2");
}
// "Date1 is before Date2"

Naturally, in real-world applications such as scheduling, financial systems, and others, we'll encounter more intricate challenges.

These all require date comparison, and an extension of the method example as above, will help you to tackle different project needs.

Parsing Dates

Parsing dates refers to the process of converting a string representation of a date into a Date object in JavaScript. One example would be with the Date.parse() method.

The Date.parse() method takes a date string as an argument and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC, corresponding to the date and time. The input string is parsed according to the IETF-compliant RFC 2822 timestamps like so:

let dateString = "Tue, 01 Feb 2022 00:00:00 GMT";
let timestamp = Date.parse(dateString);
console.log(timestamp);

Date.parse() is generally recommended over the Date constructor, as the latter has inconsistent behavior in different implementations of JavaScript and can lead to unexpected results. The new Date(dateString) constructor is also generally discouraged, and it's recommended to use a library like Moment.js for parsing dates purposes.

Useful Resources

To supplement and expand your knowledge, we recommend checking out our Dates in JavaScript - Understanding the basics article.