0

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?

Community
  • 1
  • 1
Sai
  • 1,790
  • 5
  • 29
  • 51
  • 1
    Possible duplicate of [Is there a job scheduler library for node.js?](http://stackoverflow.com/questions/3785736/is-there-a-job-scheduler-library-for-node-js) – Iceman Jul 24 '16 at 04:53
  • @Iceman i;m trying to see if i can use laterjs for this scenario as opposed to node scheduler – Sai Jul 24 '16 at 13:52

1 Answers1

0

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 * * ?');
ArunGeorge
  • 495
  • 5
  • 11
  • say if this is the given date, 07/25/2016 and if i have to run a function exactly at 11:59PM of the given date then using laterJS what would be the expression that i should use, i'm still reading through the doc but not sure which type would fit the need well. – Sai Jul 24 '16 at 04:50
  • `later.parse.text('at 11:59 pm on the 25th day of the month');` should work for this – ArunGeorge Jul 24 '16 at 05:05
  • Ok cool, that would work, and one more last thing that i'm looking for is that 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 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... – Sai Jul 24 '16 at 05:31
  • yes. you could probably store these values in a config file. Typically, you can store the configuration value in json format in the config file. Have some code to read values from that config file and form your parse text. – ArunGeorge Jul 24 '16 at 16:06
  • I tried via later.parse.text with a dynamic date and time and it did not work out... so came up with something like this in here http://stackoverflow.com/questions/38573705/constructing-a-later-parse-recur-to-run-on-a-specific-day-month-year-time looks like i'm close but not sure how to handle the time value. – Sai Jul 25 '16 at 17:48
  • Never mind it worked, looks like later.js defaults to UTC and i need to convert that to local time – Sai Jul 26 '16 at 01:26
  • Sure will do that. Should be all set now, however one issue still i see is that i trigger the function at the specified delay via later.setTimeout and it works, suppose if there is a server restart then the setTimeout does not work, i mean after restarting the server and then after reaching the delay period the setTimeout does not trigger the function. – Sai Jul 27 '16 at 22:22