JavaScript Date Object

The JavaScript date object is a built-in data type used to display Date and time on our webpage and work on other operations involving Date and time.

Syntax

const d = new Date();

We can get the Date and time by passing different values into the date constructor. The examples below explore how we can get the Date and time to our requirement.

How to get the current Date and time in JavaScript

To get the current Date and time in JavaScript, you can use the “new Date()” constructor. It does not accept any arguments and returns the current Date and time.

const currdt = new Date();
console.log(currdt);

Output

2022-03-31T07:07:47.262Z

How to get custom date and time by different parameter

To get the custom date and time by a different parameter in JavaScript, pass the different parameters.

const newdt = new Date(2000, 5, 3, 11, 43, 21, 0);
console.log(newdt);

Output

2000-06-03T06:13:21.000Z

How to get Date and time by a string parameter

To get the Date and time by a string parameter in JavaScript, pass the custom date string.

const dt = new Date("aug 1, 2011 10:03:01");
console.log(dt);

Output

2011-08-01T04:33:01.000Z

How to get Date and time by milliseconds in JavaScript

To get the Date and time by milliseconds in JavaScript, pass the millisecond to the new Date () constructor.

const d = new Date(90000000);
console.log(d);

Output

Fri Jan 02 1970 06:30:00 GMT+0530 (India Standard Time)

JavaScript Date Object methods

After creating a date object, use these methods to set and get the month, day, year, hour, minute, milliseconds, seconds, etc.

JavaScript toDateString()

The toDateString() is a built-in JavaScript method that returns the date portion of a Date object in English in the following format separated by spaces:

  1. The first three letters of the weekday name.
  2. The first three letters of the month’s name.
  3. Two-digit day of the month, padded on the left a zero if required.
  4. Four-digit year (at least), padded on the left with zeros if needed.

The toDateString() method is helpful when displaying a date in an html document.

const d = new Date();
console.log(d.toDateString());

Output

Thu Mar 31 2022

JavaScript toISOString()

The toISOString() is a built-in JavaScript method that returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively).

const d = new Date();
console.log(d.toISOString());

Output

2022-03-31T09:59:45.647Z

Other useful methods

It has many different methods. In the below example, we show the most valuable methods.

const d = new Date();

// Methods
console.log("date : ",d.getDate());
console.log("hour : ",d.getHours());
console.log("day : ",d.getDay());
console.log("month : ",d.getMonth());
console.log("minutes : ",d.getMinutes())
console.log("year :",d.getFullYear());

Output

date : 31
hour : 15
day : 4
month : 2
minutes : 31
year : 2022

That’s it for this tutorial.

Leave a Comment