This is of of the most useful JavaScript constructor. Lets get started on using it.

 

The Date() functions may take no parameters at all or may take few parameters. Let start by taking some example:

 

var date = new Date();
console.log(date) // 2018-03-30T06:53:06.534Z

The code above returns the current date and time.

A more friendlier way to print the date will be this:

 

console.log(date.toString()) //Fri Mar 30 2018 12:25:11 GMT+0530 (IST)

Thats the time when I executed this code.

 

Okay,now let us see all the different parameters that we can pass to the Date() function.

 


Date(Year, Month, Day, Hour, Minutes, Seconds, Milliseconds)

 

The Month can range from 0 to 11, 0 being January,11 being December.

 

Now, we can print about any date we like:

 

var date = new Date('2018', '11', '31');
console.log(date.toString()) // Mon Dec 31 2018 00:00:00 GMT+0530 (IST)

Oopps, 31st December 2018 is a Monday 🙁

 


 

Methods of Date() function

 

var date = new Date('2018', '11', '31');
console.log(date.getMonth()) // 11
console.log(date.getDay()) // 1

 

getDay() returns 1.
0 refers to Sunday, 6 refers to Saturday. So, 1 refers to Monday.

 

Other objects include setMonth(), toString(), parse()

 


1

CT

Hello All! You are reading the author(s) profile of CorvineLabs Team. We may be 1,2 or ..any in number. Let that remain withing us. You enjoy our write up. If you want to join the team than do Contact Us.

Leave a Reply

Your email address will not be published. Required fields are marked *