I have numerous scheduling task functions that rely on numerous convenience variables for datetime values in my project e.g. imagine 10 + lines like this:
let today = moment(),
yesterday = moment().subtract(1, 'days'),
startOfToday = moment(today).startOf("day"),
endOfToday = moment(today).endOf("day");
I can't put this kind of initialization code at the top of the myCode.js file as it leads to a bug -- the functions within myCode.js will reference stale datetimes. e.g. today will always refer to the datetime the node server started.
For now I'm initializing dates within each function, but this leads to code duplication.
Is there a cleaner way to do this?
Options I'm considering:
a) Create a function that initializes all commonly used dates and invoke that at the top of the function.
b) inline the moment.js datetime values and get rid of the convenience variables altogether (works but makes code harder to read).
Tips appreciated,
Thanks,
-S. Arora