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)
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