I was wondering if there is a way of writing the appended function in a shorter way. The only difference between the code that's executed in the conditions is whether the datetime will be set to 00:00:00 (startOf('day')) or 23:59:59 (endOf('day')). This small difference led me to believe that there must be some way of dynamically setting the 3rd function call, but I don't know how.
function moveDateForOneMonth(date, type) {
if (type == 'startOf') {
return DateTime.fromJSDate(new Date(date))
.plus({ month: 1 })
.startOf('day')
.toMillis();
} else {
return DateTime.fromJSDate(new Date(date))
.plus({ month: 1 })
.endOf('day')
.toMillis();
}
}
The type parameter is either called with an argument 'endOf' or 'startOf', which corresponds to the name of the method that should be executed once .plus({month: 1}) is added to the date.
It'd be ideal if I could do something like:
function moveDateForOneMonth(date, type) {
return DateTime.fromJSDate(new Date(date))
.plus({ month: 1 })
.[type]('day')
.toMillis();
}
I apologise for my bad formatting and thank you for the answer in advance!