javascriptnode.jsdatedatetimelaterjs

javascript - run a function at midnight of a given date


I need to run a JS function at midnight of a given date. Is that something possible with JS, mine is a node app and i was looking at the laterJS library but not sure how exactly i can use the recurse function from laterJS library for this need. It would be nice to use laterJS lib for this requirement as opposed to using something like node scheduler.

Please share any thoughts/ideas.

With this question i'm particularly looking to use a library that can do this like laterJS and not a CRON kind of thing that node scheduler provides.

The value of given date is completely dynamic and it could be any date in MM/DD/YYYY format so fundamentally i cannot hard code the value in later.parse.text as 25th day of the month etc.. but i can hard code the time as 11:59 pm, given the fact that the date is dynamic in MM/DD/YYYY format can i use later.parse.text('at 11:59 pm on the "+DD+"th day of "+MM+" in "+YYYY+"'); something like this...

So tried some thing like this, Constructing a later.parse.recur() to run on a specific day/month/year/time but not sure how to handle the time.. Any help?


Solution

  • May be Cron expression section of Later.js is what you are looking at. It is meant for exactly this sort of thing.

    You can pick the relevant one from below -

    If you want on a specific day of the month, the expression will be something like

    var complexSched = later.parse.recur()
                  .on(15).dayOfMonth().onWeekday().on(2).hour()
    

    If it is a repeating schedule, it will be something like

    var textSched = later.parse.text('at 10:15am every weekday');
    

    A cron expression will be something like this-

    var cronSched = later.parse.cron('0 0/5 14,18 * * ?');