Menu Close

JavaScript Date Object

In this guide, you will learn about JavaScript Date Object. Date in JavaScript is used to make easier when you are going to work with date related task. In previous, tutorial we have seen all about JavaScript number and number methods with the help of the example.

JavaScript Date Object

By default, JavaScript will use browser by default date and display as a full string but sometimes we want to work with date, Then we can use JavaScript date object. Date object has lots of properties and methods available which make it easier to work with a date.

Create Date Object

JavaScript Date object created using with new Date() constructor. There are four ways to create data in JavaScript.

  • new Date()
  • new Date(year, month, day, hours, minutes, seconds, milliseconds)
  • new Date(milliseconds)
  • new Date(date string)

The new Date()

The new Date() is used to create a new date object with current date and time.

Example:


var d = new Date();
console.log(d); 

The new Date(year, month, day, hours, minutes, seconds, milliseconds)

The new Date(year, month,…..) is used to create new date object with specific date and time.

Example:


var d = new Date(2019, 11, 25, 10, 33, 20, 15);
console.log(d);

Note: JavaScript count month 0 to 11.January is 0 and December is 11.

6 Parameter specify year, month, day, hours, minutes, seconds.

Example:


var d = new Date(2019, 11, 25, 10, 33, 20);
console.log(d);

5 Parameter specify year, month, day, hours, minutes.

Example:


var d = new Date(2019, 11, 25, 10, 33);
console.log(d);

4 Parameter specify year, month, day, hours.

Example:


var d = new Date(2019, 11, 25, 10);
console.log(d);

3 Parameter specify year, month, day.

Example:


var d = new Date(2019, 11, 25);
console.log(d);

2 Parameter specify year, month.

Example:


var d = new Date(2019, 11);
console.log(d);

You cannot omit month. If you supply only one parameter it will be treated as milliseconds.

The new Date(date string)

The new Date(date string) is used to create new date object with date string.

Example:


var d = new Date("Mon May 04 2020 13:04:13");
console.log(d);

The new Date(milliseconds)

The new Date(milliseconds) is used to create new date object with milliseconds.

Example:


var d = new Date(10);
console.log(d);

Date Methods

When a date object is created, JavaScript provide lots of date method, which are used to work with date object.

The toString()

The toString() is used to convert date into string.

Example:


var d = new Date().toString();
console.log("Date is:- ", d);

The toUTCSTring()

The toUTCSTring() string is used convert a date to UTC string.

Example:


var date = new Date().toUTCString();
console.log(`Date in UTC string :- ${date}`);

The toDateString()

The toDateString() is used to convert a date to more readable format.

Example:


var date = new Date().toDateString();
console.log(`Date in UTC string :- ${date}`);

The toISOString()

The toISOString() method is used to convert a date to string using the ISO standard format.

Example:


var date = new Date().toISOString();
console.log(`Date in UTC string :- ${date}`);

Summary

In the JavaScript Date tutorial, you have explained all about JavaScript date object with the help of the corresponding examples. If you’ll working on a real-life work project, then the date object is going to very for you.

I hope this tutorial will have helped you. if you like this article, please share and keep visiting for the next JavaScript tutorial.

JavaScript Random
JavaScript Date Get Methods

Related Posts