?
Solved

working with date and manipulation of dates added to database

Posted on 2007-07-27
9
Medium Priority
?
193 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:lobos
  • 4
  • 4
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19583412
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
0
 

Author Comment

by:lobos
ID: 19583461
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?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19583488
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:lobos
ID: 19593992
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19595001
If you want once every week:
  sDate.AddDays(7)

once a month:
  sDate.AddMonths(1)

once a year:
  sDate.AddYears(1)

Bob
0
 

Author Comment

by:lobos
ID: 19595344
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?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 19595543
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
0
 

Author Comment

by:lobos
ID: 19754317
thanks for the reminder and the help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question