Re:Ask HN: Why isn't there a `setDay` method on the Date pro...
by CyberFonic on Monday September 19, 2016 @12:25AM
(#12528898)
console.log(today.getDay()) // prints 1
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.
Re:Ask HN: Why isn't there a `setDay` method on the Date pro...
by saghm on Sunday September 18, 2016 @10:24PM
(#12528388)
But `getDate` is separate from `getDay`, so why isn't there a `setDay` as well as a `setDate`?
Re:Ask HN: Why isn't there a `setDay` method on the Date pro...
by nicomfe on Sunday September 18, 2016 @10:34PM
(#12528426)
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
Re:Ask HN: Why isn't there a `setDay` method on the Date pro...
by saghm on Sunday September 18, 2016 @10:40PM
(#12528449)
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
Re:Ask HN: Why isn't there a `setDay` method on the Date pro...
by CyberFonic on Monday September 19, 2016 @12:28AM
(#12528908)
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.
Re:Ask HN: Why isn't there a `setDay` method on the Date pro...
by saghm on Monday September 19, 2016 @08:32PM
(#12535983)
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
Re:Ask HN: Why isn't there a `setDay` method on the Date pro...
by nicomfe on Monday September 19, 2016 @01:08AM
(#12529087)
cause if you have the date 19/09/2016 whats the point of setting the day of the week? so do you wanna set the date to be Thursday but still keep the 19/09/2016 that wouldnt make any sense, cause 19/09/2016 is Monday, and in all cases must be Monday.
Re:Ask HN: Why isn't there a `setDay` method on the Date pro...
by saghm on Monday September 19, 2016 @08:33PM
(#12535990)
What if I wanted to get the day at the beginning of the week that a date occurred in? For instance, if you had a payroll system where you paid hourly workers weekly, it would be useful to be able to bucket the hours worked on the day that the paycheck is sent
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