You can get the date using the Date Object. Here's an example below which I will type in to the console.
let currentDate = new Date(); currentDate; //Output is: Sun Jul 12 2020 20:00:37 GMT-0400 (Eastern Daylight Time)
If you test this out in the console, it will look similar to this:
As a reminder, even though the date looks like a string, it is actually a date object that is returned to us.
From the date object, we can select specific information we want, such as the day.
We call the
getDay() method on the date object which will return a number assigned to the day of the week. 0 for Sunday through 6 for Saturday.
currentDate.getDay(); //Output is: 0 0 stands in for Sunday.
There are various methods similar to
getDay() to pull different info from the date object.
- getMonth() for the month (range is 0-11)
- getDate() for which day of the month (range is 1-31)
- getFullYear() for the year
- getHours() for the hour (range is 0-23. 0 is midnight and 23 is 11pm)
- getMinutes() for the minute (range is 0-59)
- getMilliseconds() for the millisecond (range is 0-999)
- getTime() for the number of milliseconds since Jan 1, 1970.
Try these methods out to get the date and time information you want to display.
Top comments (0)