Lesson 24 of 36 · 4 min

The Date Object

The JavaScript Date object is a fundamental tool for working with dates and times in web development. It allows developers to manage dates, perform…

On this page
  1. Introduction to the JavaScript Date Object
  2. Creating a Date Object
  3. Date Methods :

The JavaScript Date object is a fundamental tool for working with dates and times in web development. It allows developers to manage dates, perform calculations, and format dates for display. In this guide, we’ll explore the various aspects of the Date object and its practical applications.

Introduction to the JavaScript Date Object

The Date object is an essential part of JavaScript’s standard library. It represents a specific point in time, including the date, time, and timezone information. With the Date object, you can perform a wide range of operations, from simple date display to complex date arithmetic.

Creating a Date Object

You can create a Date object in several ways. The most common method is by invoking the new Date() constructor without any arguments, which gives you the current date and time:

const currentDate = new Date();

You can also create a Date object for a specific date and time by passing a date string to the constructor. The date string should be in ISO format ('YYYY-MM-DDTHH:mm:ss.sssZ'):

const specificDate = new Date('2023-10-15T14:30:00.000Z');

Date Methods :

1. getDate(): Returns the day of the month (1-31) for a given date.

const date = new Date('2023-10-15');
const day = date.getDate(); // 15

2. getDay(): Returns the day of the week (0-6) for a given date, where 0 represents Sunday and 6 represents Saturday.

const date = new Date('2023-10-15');
const dayOfWeek = date.getDay(); // 6 (Saturday)

3. getFullYear(): Returns the year (e.g., 2023) for a given date.

const date = new Date('2023-10-15');
const year = date.getFullYear(); // 2023

4. getHours(): Returns the hour (0-23) for a given date.

const date = new Date('2023-10-15T14:30:00');
const hour = date.getHours(); // 14

5. getMilliseconds(): Returns the milliseconds (0-999) for a given date.

const date = new Date('2023-10-15T14:30:45.123');
const milliseconds = date.getMilliseconds(); // 123

6. getMinutes(): Returns the minutes (0-59) for a given date.

const date = new Date('2023-10-15T14:30:45');
const minutes = date.getMinutes(); // 30

7. getMonth(): Returns the month (0-11) for a given date, where 0 represents January and 11 represents December.

const date = new Date('2023-10-15');
const month = date.getMonth(); // 9 (October)

8. getSeconds(): Returns the seconds (0-59) for a given date.

const date = new Date('2023-10-15T14:30:45');
const seconds = date.getSeconds(); // 45

9. getTime(): Returns the timestamp, which is the number of milliseconds since January 1, 1970 (Unix epoch), for a given date. Useful for comparing dates.

const date = new Date('2023-10-15T14:30:00');
const timestamp = date.getTime(); // 1694886600000

10. getTimezoneOffset(): Returns the time zone offset in minutes between the local time and UTC (Coordinated Universal Time) for a given date. Positive values represent time zones west of UTC, while negative values represent time zones east of UTC.

const date = new Date('2023-10-15');
const offsetMinutes = date.getTimezoneOffset(); // -240 (Eastern Daylight Time)

11. setDate(day): Sets the day of the month (1-31) for a given date.

const date = new Date('2023-10-15'); 
date.setDate(20); // Sets the date to October 20, 2023

12. setFullYear(year): Sets the year for a given date.

const date = new Date('2023-10-15'); 
date.setFullYear(2024); // Sets the year to 2024

13. setHours(hours): Sets the hour (0-23) for a given date.

const date = new Date('2023-10-15T14:30:00'); 
date.setHours(16); // Sets the hour to 16

14. setMilliseconds(milliseconds): Sets the milliseconds (0-999) for a given date.

const date = new Date('2023-10-15T14:30:45'); 
date.setMilliseconds(500); // Sets the milliseconds to 500

15. setMinutes(minutes): Sets the minutes (0-59) for a given date.

const date = new Date('2023-10-15T14:30:00'); 
date.setMinutes(45); // Sets the minutes to 45

16. setMonth(month): Sets the month (0-11) for a given date.

const date = new Date('2023-10-15'); 
date.setMonth(11); // Sets the month to December (11)

17. setSeconds(seconds): Sets the seconds (0-59) for a given date.

const date = new Date('2023-10-15T14:30:45'); 
date.setSeconds(15); // Sets the seconds to 15

18. setTime(timestamp): Sets the date and time for a given date using a timestamp (milliseconds since the Unix epoch).

const date = new Date(); 
date.setTime(1694886600000); // Sets the date and time based on the provided timestamp

19. toLocaleDateString(): Returns a string representing the date portion of a given date, formatted according to the user’s locale and time zone settings.

const date = new Date('2023-10-15'); 
const formattedDate = date.toLocaleDateString(); // Depends on user's locale and time zone

20. toLocaleTimeString(): Returns a string representing the time portion of a given date, formatted according to the user’s locale and time zone settings.

const date = new Date('2023-10-15T14:30:00'); 
const formattedTime = date.toLocaleTimeString(); // Depends on user's locale and time zone

21. toLocaleString(): Returns a string representing both the date and time of a given date, formatted according to the user’s locale and time zone settings.

const date = new Date('2023-10-15T14:30:00'); 
const formattedDateTime = date.toLocaleString(); // Depends on user's locale and time zone

22. toString(): Returns a string representing a given date in a human-readable format.

const date = new Date('2023-10-15T14:30:00'); 
const dateString = date.toString(); // "Sat Oct 15 2023 14:30:00 GMT+0000 (Coordinated Universal Time)"

These Date methods provide you with a wide range of functionalities to manipulate, extract

More from JavaScript Tutorial