Link to home
Start Free TrialLog in
Avatar of lobos
lobos

asked on

working with date and manipulation of dates added to database

I am creating a C# web calendar of activities. And working on this repeating a day's event on a calendar type of idea, and for each day I insert a record to a database.
So I have a start date and end date and use the following logic to determine the amount of days(records) to
insert. (each record is a day).


DateTime sDate = DateTime.ParseExact(startDate, "M/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime eDate = DateTime.ParseExact(endDate, "M/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);

int numTimesToRepeat = ((TimeSpan)(eDate - sDate)).Days;
numTimesToRepeat = numTimesToRepeat +  1;

numTimesToRepeat is the amount of times I insert a record to the database and increment the date by one...

sDate.AddDays(i)

The question I have is how do I do this if I only want to insert a record for say every other day..so it would starting with the start date day, and then every other day until the end day. (only to include week days). ie. Mon, Wed, Fri

Also I want to insert a record for every first monday of the month. So it would starting with the start date day, and then every first Monday of the month until the end date.

I'm not completely familiar with the date feature and how to accomplish what I need, but I am pretty sure this can be done.
Any assistance would be greatly apprecited.

I will increase the points if required
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

The DateTime structure has the DayOfWeek property that can tell what day of the week a date is.  With that you call call AddDays(1) when you get to Sunday to skip to Monday.

Bob
Avatar of lobos
lobos

ASKER

Ok that helps but and makes sense but if there a way to do this, 'every other day concept', would it mean checking that property..if Monday then add to db, or else skip a day?
Is there anything else built in that I can capitalize on?
I could see something like this (untested):

            while (sDate < eDate)
            {
                while (sDate.DayOfWeek == DayOfWeek.Saturday || sDate.DayOfWeek == DayOfWeek.Sunday)
                    sDate = sDate.AddDays(1);

                // Write record.

                // Skip a day
                sDate = sDate.AddDays(2);
            }

Bob
Avatar of lobos

ASKER

Ok that seems like it would work for one scenario, but what if I wanted only for once every week like every first monday of the month until a specified date.
I need to apply this concept for 'every month', and 'every year'.
I dont think the logic/concept mentioned above will work for this idea will it?

I look forward to your input.

thanks.
If you want once every week:
  sDate.AddDays(7)

once a month:
  sDate.AddMonths(1)

once a year:
  sDate.AddYears(1)

Bob
Avatar of lobos

ASKER

ok that makes sense (duh, why didn't I think of that)...but the problem is, when I want it for once a month and once a year...it needs to be on the same 'weekday' as opposed the numerically day for each (week, month or year).
Its a like a monthly meeting once a month on the tuesday of the first week of the month type of thing...so each month the 'date' might differ from month to month, but the weekday is the same.
Does that make sense?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lobos

ASKER

thanks for the reminder and the help.