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...


The question I have is how do I do this if I only want to insert a record for say every other 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
Who is Participating?
Bob LearnedCommented:
If your current date is Monday, and you add 7 days, it will be Monday on the next week.  

If you want to increment to next month or year, and stay on the same week day, then you need to increment the day by 1 until you get to the day of the week that you want.

Bob LearnedCommented:
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.

lobosAuthor Commented:
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?
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Bob LearnedCommented:
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);

lobosAuthor Commented:
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.

Bob LearnedCommented:
If you want once every week:

once a month:

once a year:

lobosAuthor Commented:
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 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 each month the 'date' might differ from month to month, but the weekday is the same.
Does that make sense?
lobosAuthor Commented:
thanks for the reminder and the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.