Table of Contents
We could create a date object in four different ways
new Date() new Date() creates a new date object with the current date and time: Example
new Date(year, month, ...) new Date(year, month, ...) creates a new date object with a specified date and time.
7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):
new Date(milliseconds) creates a new date object as zero time plus milliseconds:
One thing to keep in mind about the date object is that it is a static object.
Now that we have looked at the different methods of creating a date object, we shall use the first method and get the specific details we need.
Get Current YeargetFullYear() method can be used to get current year from the current date object
Get Current Month:Similar to the year, we can also get the current month using the getMonth() method.
As mentioned before, the months are calculated from 0 to 11 instead of 1 to 12. To get an output in a readable format, we could just add 1 to the output.
Get Current Date:The getDate() method can be used to get the day of the month.