0

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()));
adiga
  • 34,372
  • 9
  • 61
  • 83
  • 1
    The [**getDay()**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) method returns the day of the week for the specified date according to local time, where 0 represents Sunday. – palaѕн May 28 '20 at 04:54
  • @JackBashford I've mentioned this many times before. Please don't add unrelated tags to earn badges. The ecmascript-6 tag wiki clearly says *"Only use this tag where the question specifically relates to new features or technical changes provided in ECMAScript 2015*". The question is about simple bracket notation. – adiga May 28 '20 at 05:00
  • 1
    For more details, please check [Day Name from Date in JS](https://stackoverflow.com/q/24998624) and [Get month name from Date](https://stackoverflow.com/q/1643320) – palaѕн May 28 '20 at 05:01
  • Apologies @adiga - I was looking at the wrong area. My bad. – Jack Bashford May 28 '20 at 05:18

2 Answers2

1

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"]
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

It seems that d is a Date object.

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

cj81499
  • 76
  • 1
  • 6