Ask HN: Why isn't there a `setDay` method on the Date prototype?
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://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), it appears that this wasn't an oversight of the shell implementation but seemingly part of the spec. To make sure thi wasn'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.
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't think of any reason why the method should be omitted, but maybe I'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.
15 comments
[ 3.3 ms ] story [ 39.8 ms ] threadvar 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
getDay returns 1 (Sunday=0, Monday=1, ....)
getDate returns 19
assuming there was a setDay, and you invoke:
today.setDay(3)
should today now be: 21/9/2016 or 14/9/2016 or 19/10/2016 or 19/9/2040 or ... ?
I think you can see that unlike the other setters, setDay would not have an "obvious" implementation.