3

This is for a system that essentially allows you to set the first date for a given event, then to set the recurrence period.

Eg. I set a date for a week from now, 19/07/2012, so I know that I have to put the cat out with the milk. I also set it to be a weekly notification, so in future weeks I want to be notified of the same.

That original date sits in my database, which is fine for week 1, but in week 2 I need to return the date as the original plus 1 week.

On the face of it, that may seem straightforward, but I need to make sure I can account for leap years and different recurrence frequencies (fortnightly, monthly, yearly, whatever).

I'd like to keep this as a javascript implementation - because it's quicker and I feel probably would require less code than updating dates in the database. Maybe it's not achievable, any pointers would be excellent.

I think these may be a starting point:

Given a start date , how to calculate number of years till current date in javascript

Given a date, how can I efficiently calculate the next date in a given sequence (weekly, monthly, annually)?

Update, I've written the below to return the amount of time to add in each different case, from there I can just use the answer below:

var strDate = $(this).find('.next').text();
            var frequency = $(this).find('.occurs').text();
            var frmDate = getDateObject(strDate);
            var toDate = new Date();

            var days = parseInt(Math.floor((frmDate - toDate) / 86400000));

            if(days < 0) {
                // find out how many WHOLE 'frequencies' have passed
                var weeks = Math.ceil(0 - (days / 7));
                var months = Math.ceil(0 - (monthDiff(toDate,frmDate)));
                var years = Math.ceil(months / 12);
                //alert(days + '/' + weeks + '/' + fortnights + '/' + months + '/' + quarters + '/' + years);

                if(frequency == 'Weekly') { frmDate.add(weeks).weeks(); }
                if(frequency == 'Fortnightly') { frmDate.add(weeks*2).weeks(); }
                if(frequency == 'Monthly') { frmDate.add(months).months(); }
                if(frequency == 'Quarterly') { frmDate.add(months*3).months(); }
                if(frequency == 'Annually') { frmDate.add(years).years(); }

                var newdate = frmDate.toString("dd/MM/yyyy");
                //alert(newdate);

                $(this).find('.next').text(newdate);
            }
Community
  • 1
  • 1
Midwinter
  • 48
  • 6
  • Can I see the demo of this if you had completed it in 2012? :) I came across the same problem and had issues with implementing the recurrence part of it. Any help will be grateful. Thanks. – Superman Jan 04 '15 at 05:18

3 Answers3

0

What I would do (probably not the best solution, I'm just coming up with it right now) is to start from the initial date and use a loop: while the date you are observing is less than the current date, increment the observed date by a week (fortnight, month, year etc.). If you land on the current date, the event happens. Otherwise it's for another day.

You can use things like date.setDate(date.getDate()+1); to increment the date by a day, the same +7 for a week, using set/getMonth and set/getFullYear for months and years respectively. If you give a value out of bounds, JS will wrap it (so March 32nd becomes April 1st)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I think with javascript I could take two date objects, one for today, one for the original date, and subtract one from the other to get the time between. Then I could return that amount of time in weeks (or months or years etc) and add one? – Midwinter Jul 12 '12 at 13:09
0

Also, the SQL implementation for this would be using DATEADD:

http://sql-plsql.blogspot.com/2010/07/dateadd.html

You don't have to worry about special dates like leap year and so forth, because most Date functions take care of that.

Alternatively, you can use the getDate(), getMonth() as the other user suggested. var today = new Date(); today.setDate(today.getDate() + numberOfDaysToAdd);

roymustang86
  • 8,054
  • 22
  • 70
  • 101
  • The key here though, would be knowing how many days to add I think. So, given a weekly schedule on 12/07/2012, do I count how many whole weeks have occurred since that first date to today, and then add that many weeks + 1? – Midwinter Jul 12 '12 at 13:06
  • wait, you want to find number of weeks between two given dates? is that your question? – roymustang86 Jul 12 '12 at 13:34
  • No, but that would be a requirement given that you wouldn't know how much time to add, unless you were to calculate that first... unless I'm completely in the wrong forest here. – Midwinter Jul 12 '12 at 14:04
0

Please check out the following code for some raw idea

    var someDate = new Date();
for(var i = 0 ; i < 7 ; i++)
    {
someDate.setDate(someDate.getDate() + 1);   
        console.log(someDate)
    }

You can test the same in the below fiddle

Consecutive 7 days from current day

prodeveloper
  • 943
  • 14
  • 22