
Ask HN: Why isn't there a `setDay` method on the Date prototype? - saghm
Recently, I was using a JavaScript shell and was playing around with a `Date` object. When looking at the various methods, I saw getters for various parts of the date (`getSeconds`, `getMinutes`, `getHours`, `getDay`, `getMonth`, `getYear`, etc.). However, when looking at the setter methods available, I noticed that while there are methods `setSeconds`, `setMinutes`, and analogues for most of the getters, there is no `setDay` analog for `getDay`. From looking at the Mozilla documentation (https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Date), it appears that this wasn&#x27;t an oversight of the shell implementation but seemingly part of the spec. To make sure thi wasn&#x27;t just a Mozilla thing, I tried checking for the method in Node and in the Chrome console; neither of them had a `setDay` method.<p>Does anyone know what the historical reason is for the lack of a `setDay` method? Was it a mistake in an early implementation of the language that just got propagated through until today? Was it even a mistake at all? I can&#x27;t think of any reason why the method should be omitted, but maybe I&#x27;m missing something. From googling, I can only find a few references to the `setDay` method being missing, and none of them have any explanation for this.
======
nness
Maybe its just to avoid confusion with date handling? Seems that a call to
setDay would really just be a call to setDate with +/\- 6 days.

------
perfmode
If you were to set the day, what would be the resulting date? Would you move
forward or backward in time?

~~~
saghm
I don't see how that would be any less hard to define than `setMonth` or
`setHour`

~~~
perfmode
Set hour is easier because it has an obvious result. The date doesn't change.

~~~
saghm
So setting the day of the week would set to that day in the same week the date
occurred; I don't think that's any weirder than calling `setMonth` to change
the month on a date

------
nicomfe
it is called setDate , i know, a bit confusing haha

var today = new Date() // today is 19/09/2016

console.log(today.getDate()) // will print 19

today.setDate(20)

console.log(today.getDate()) // will print 20

~~~
saghm
But `getDate` is separate from `getDay`, so why isn't there a `setDay` as well
as a `setDate`?

~~~
nicomfe
getDay returns the index of the week day your date is getDate returns the day
number within the month so for 19/09/2016

getDay returns 1 (Sunday=0, Monday=1, ....)

getDate returns 19

~~~
saghm
So why not have a method that sets the day of the week as well? It just seems
odd to me that they would implement setters for all of the other time units
but not this one

~~~
CyberFonic
See my comment above. What interpretation would you prefer? setDay changes the
date into the future or past by changing the date, the month or the year. At a
minimum there are 6 possible values. To me none of them are an obvious choice.

~~~
saghm
Given that `setMinute` sets the minute in the same hour and `setHour` sets the
hour in the same day, I'd think `setDay` setting to the day in the same week
would be rather logical

