Question

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.

Was it helpful?

Solution

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));
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top