Reputation: 1
I am a beginner and recently I have stumbled onto this. I do not understand what this d.getDays()
does. Please help me out.
const dateBuilder = (d) => {
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
let day = days[d.getDay()];
let date = d.getDate();
let month = months[d.getMonth()];
let year = d.getFullYear();
return `${day} ${date} ${month} ${year}`
}
console.log(dateBuilder(new Date()));
Upvotes: 0
Views: 137
Reputation: 76
It seems that d is a Date object.
getDay() is a method being called on that object.
Upvotes: 1
Reputation: 44125
It's a method of the Date
object that returns a number representing the day of the week (Monday, Tuesday etc.) that correlates to the days
array you have in your code. Read more here. It's basically getting the "name" of the day by using that array days
:
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
Upvotes: 1