1

How can i execute an alert every week starting now using moment.js?

something like a situation of starting now or getting the date now, and then alert it every week or every 7th day starting today using the moment.js library. Or i want to execute a function every week or every 7th day starting from getting the date now.

Jc John
  • 1,799
  • 2
  • 34
  • 69
  • You could set an alarm clock. Why must you use moment.js? It's very easy to see if today is 7 days after some other date or not, see [*Get difference between 2 dates in javascript*](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript?s=2|2.2910) or [*How do I get the number of days between two dates in JavaScript*](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) or any one of dozens of similar questions. What have you tried? – RobG Jun 20 '17 at 02:04
  • @RobG . i have a function to be executed yearly or weekly. starting this day to a year or a week. – Jc John Jun 20 '17 at 02:08
  • How will you remember the start date? Local storage and cookies are extremely unreliable, so this seems like a server task (which might still be javascript, but may be very different to on a client). – RobG Jun 20 '17 at 02:09
  • @RobG do you mean, i want to save the current date from the database? – Jc John Jun 20 '17 at 02:13
  • You say "from now", so how will you remember what "now" is/was? I assume it will be different for each user. – RobG Jun 20 '17 at 02:19
  • @RobG .. thanks for your reply. please help me to make formula using moment js to be simplify?. like monthly or weekly? pls. thanks – Jc John Jun 20 '17 at 02:20
  • @RobG how can i something make a condition to run a function if it is already 7days from now or a month from now? please answer thanks.. – Jc John Jun 20 '17 at 02:21
  • Are you talking about an `alert()` in the browser, or "alert" in the more generic "notification" sense to be generated by NodeJS, or...? – nnnnnn Jun 20 '17 at 03:01
  • @nnnnnn thanks for reply .. just a simple alert from a javascript.. because i will replace it with a function – Jc John Jun 20 '17 at 03:27
  • If it's in the browser, then what do you expect to happen if your page isn't open in the browser at the time the alert was due? – nnnnnn Jun 20 '17 at 03:28
  • the alert was due by weekly. or for example, i want to select weekly. then from the 7th day after i select, it will alert automaticaly exactly 7th day including hrs:mns:seconds. . btw, thanks for your reply @nnnnnn – Jc John Jun 20 '17 at 03:33
  • Yes, and exactly seven days later, what if your computer is turned off? Should you just forget that alert and show the one the following week if the computer is back on by then and your page is open in the browser, or should you show the missed alert as soon as possible after its scheduled time if the user opens your page the day after, or...? – nnnnnn Jun 20 '17 at 03:44
  • @nnnnnn thanks for reply again. its ok to missed the alert. what i want is to run my function in it – Jc John Jun 20 '17 at 04:00

1 Answers1

2

Instead of using moment.js you could use node-cron: https://www.npmjs.com/package/node-cron.

Then running something like:

var cron = require('node-cron');
var moment = require('moment');

//Gets current day of week (1-7), 1 being Monday
var dayOfWeek = moment().isoWeekday(); 

cron.schedule('* * * * dayOfWeek', function(){
    console.log("Running every week on current day")
    // Do other stuff here
});
bmpickford
  • 90
  • 1
  • 9