0

Is there an algorithm for weekly reminders ?

For example, I set a reminder for Thursday & I check the "weekly" option.
The Reminder is supposed to alert every Thursday then,but how is this done?

I thought about an idea, but I guess it's very stupid:

  1. Get today's "day".
  2. Get today's "date".
  3. Get the wanted day number.
  4. Subtract both days from each other.
  5. using [4] get that day's date.
  6. Increment the counter after every alert with 7.

I don't even know whether this will work or not, I'm sure there is a better way to do it, so I need your opinions before starting implementation.

PS: I use JavaScript so the functions are very limited.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Sana Joseph
  • 1,948
  • 7
  • 37
  • 58
  • 1
    Think about your question and what context it needs to be answered. You might have that knowledge, but unless you put it down coherently as a defined question it's very hard to answer specially. – Preet Sangha Dec 12 '12 at 12:09
  • 1
    can you be more specific as to what you are trying to accomplish? For example, if you want a function to be called every seven days, you could simply do a `setTimeout(myFunction, 3600*24*7);` at the end of it. – Manuel Schweigert Dec 12 '12 at 12:10

1 Answers1

2

It's not entirely clear what you're trying to do.

  1. If your code needs to know whether it's Thursday, that's really easy using getDay, which gives you the day of the week:

    if (new Date().getDay() === 4) {
        // It's Thursday
    }
    

    The day numbers start with 0 = Sunday.

  2. If your code needs to find the next Thursday starting on a given date:

    var dt = /* ...the start date... */;
    while (dt.getDay() !== 4) {
        dt.setTime(dt.getTime() + 86400000)) // 86400000 = 1 day in milliseconds
    }
    

    or of course without the loop:

    var dt = /* ...the start date... */;
    var days = 4 - dt.getDay();
    if (days < 0) {
        days += 7;
    }
    dt.setTime(dt.getTime() + (days * 86400000));
    
  3. If you have a Thursday already and you need to know the date for the next Thursday:

    var nextThursday = new Date(thisThursday.getTime() + (86400000 * 7));
    
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875