emigoodman
emigoodman

Reputation: 1

Can anyone tell me how this javaScript function works please?

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

Answers (2)

cj81499
cj81499

Reputation: 76

It seems that d is a Date object.

getDay() is a method being called on that object.

Upvotes: 1

Jack Bashford
Jack Bashford

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

Related Questions