This guide will help you learn all about JavaScript Date concepts and methods in detail with lots of code examples.

JavaScript Date Complete Tutorial
JavaScript Date Complete Tutorial

JavaScript is a popular programming language that provides built-in functionality for working with dates and times.

The Date object in JavaScript allows developers to work with dates and times in a variety of ways.

In this article, we will explore the Date object, its methods, syntax, and how it can be used to manipulate dates and times in JavaScript.

The Date Object

The Date object is used to work with dates and times in JavaScript. It provides methods to get and set various components of a date and time.

To create a Date object in JavaScript, use the new Date() constructor. You can pass parameters to the constructor to specify the date and time you want to work with.

Syntax: new Date(); 
Syntax: new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

Here’s an example of creating a Date object with the current date and time:

let today = new Date(); 
console.log(today); // Output: Wed Mar 24 2023 10:20:40 GMT-0400 (Eastern Daylight Time)

Here’s an example of creating a Date object with a specific date:

let specificDate = new Date(2023, 2, 15); 
console.log(specificDate); // Output: Tue Mar 15 2023 00:00:00 GMT-0400 (Eastern Daylight Time)

Date Methods

The Date object provides several methods to get and set the various components of a date and time. Here are some of the most commonly used methods:

1. getFullYear(): Returns the year of a date.

The getFullYear() method returns the year of the given date, as a four-digit number.

See also  Top 10 Mistakes Developers Make in Node.js Apps

For example, if the date is March 24, 2023, the method will return 2023.

Syntax: date.getFullYear();

Here’s an example of getting the current year:

let today = new Date(); 
let year = today.getFullYear();
console.log(year); // Output: 2023

2. getMonth(): Returns the month of a date.

The getMonth() method returns the month of the given date, as a zero-based index.

For example, if the date is March 24, 2023, the method will return 2, which represents March. Note that January is represented by 0, February by 1, and so on.

Syntax: date.getMonth();

Here’s an example of getting the current month:

let today = new Date(); 
let month = today.getMonth(); 
console.log(month); // Output: 2 (March)

Note that the getMonth() method returns a zero-based index, where 0 represents January, 1 represents February, and so on.

3. getDate(): Returns the day of the month of a date.

The getDate() method returns the day of the given date, as a number between 1 and 31. For example, if the date is March 24, 2023, the method will return 24.

Syntax: date.getDate();

Here’s an example of getting the current day:

let today = new Date(); let day = today.getDate(); console.log(day); // Output: 24

4. getDay(): Returns the day of the week of a date.

The getDay() method returns the day of the week of the given date, as a zero-based index.

For example, if the date is March 24, 2023, which is a Wednesday, the method will return 3, which represents Wednesday. Note that Sunday is represented by 0, Monday by 1, and so on.

Syntax: date.getDay();

Here’s an example of getting the current day of the week:

let today = new Date(); 
let weekday = today.getDay(); 
console.log(weekday); // Output: 3 (Wednesday)

Note that the getDay() method returns a zero-based index, where 0 represents Sunday, 1 represents Monday, and so on.

See also  JavaScript Arrow Functions Tutorial With Examples

5. getHours(): Returns the hours of a date.

The getHours() method returns the hours of the given date, as a number between 0 and 23.

For example, if the time is 10:20:40 AM, the method will return 10.

Syntax: date.getHours();

Here’s an example of getting the current hour:

let today = new Date(); 
let hours = today.getHours(); 
console.log(hours); 
// Output: 10

6. getMinutes(): Returns the minutes of a date.

The getMinutes() method returns the minutes of the given date, as a number between 0 and 59.

For example, if the time is 10:20:40 AM, the method will return 20.

Syntax: date.getMinutes();

Here’s an example of getting the current minute:

let today = new Date(); 
let minutes = today.getMinutes(); 
console.log(minutes); // Output: 20

7. getSeconds(): Returns the seconds of a date.

The getSeconds() method returns the seconds of the given date, as a number between 0 and 59.

For example, if the time is 10:20:40 AM, the method will return 40.

Syntax: date.getSeconds();

Here’s an example of getting the current second:

let today = new Date(); 
let seconds = today.getSeconds(); 
console.log(seconds); // Output: 40

8. getMilliseconds(): Returns the milliseconds of a date.

The getMilliseconds() method returns the milliseconds of the given date, as a number between 0 and 999. For example, if the time is 10:20:40.123 AM, the method will return 123.

Syntax: date.getMilliseconds();

Here’s an example of getting the current millisecond:

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

The Date object in JavaScript provides a variety of methods for working with dates and times.

These methods can be used to get and set the various components of a date and time, such as the year, month, day, hour, minute, second, and millisecond.

See also  Jasmine JS Interview Questions and Answers

By using these methods, developers can manipulate dates and times in a flexible and efficient way.