Another newbie question for you: How can I ensure that a variable remains constant?
I am trying to add in a new calculation to add 45 days to a start_date value (a report deadline). I try doing this by noting the value should remain constant:
report_due_gen = {
const date = start_date;
date.setDate(date.getDate() + 45);
return date
}
However, adding in this calculation changes anther calculation also based on the start date, namely:
It looks like youâre calling date.setDate() ⌠which in JavaScript mutates the value of the original start_date object, changing it in an undesired way.
Try returning a new Date() from report_due_gen insteadâŚ
Oh no! This works to separate out the date, but it also seems to decouple the calculation from the start date? I get thrown back to 1969 and +45 is adding milliseconds
Apologies, Aaron. I was missing the forest for the trees (seeing the mutation, but missing the point of what you were trying to do).
You canât just add a raw number to getDate() like that in JS, and get something meaningful back (unfortunately). For date manipulation, Iâd recommend using a library like Moment to make things a little easier.
moment = require("moment")
Then, if you still want to add 45 days to your start date, the definition would be:
I had come across moment before when I was first working on that âworking daysâ calendar a couple weeks ago. On the encouragement of @Job@bgchen and others, Iâve been trying to limit my reliance on anything other than âvanillaâ JS, so as to try to master the basics before moving too much further. With dates especially, however, a bit of help is appreciated!
Cool, @mootari! This is a great solution in that it looks like I might also be able to integrate it with the âcalculate holidaysâ function that I was working on in the notebook.