469,936 Members | 2,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,936 developers. It's quick & easy.

Calendar - Repeating Events

I have an ASP.Net calendar feature which allows users to add events and
configure whether or not they repeat at various frequencies (i.e, daily,
weekly, monthly, Sat/Sun, etc.). What I'm looking for is some C# code that
will calculate a date of "Every Other Week" based on a starting date and
ending date.

Example: If a user enters an event dated October 3, 2005 and would like it
to repeat every other week until October 31, 2005, the code will return the
following dates:

October 3, 2005
October 17, 2005
October 31, 2005

Any help would be greatly appreciated.

Rob
Nov 17 '05 #1
4 5452
I think its as simple as adding number of days to the datetime type as:

DateTime dt = new DateTime(2005, 10, 3);
dt=dt.AddDays(14);

now dt has 17th oct. You keep on adding days like that until the the end
date comes, like in a loop.

Hope that helps.

Ab.
http://joehacker.blogspot.com
"Rob Johnson" <rj**********@comcast.net> wrote in message
news:nN********************@comcast.com...
I have an ASP.Net calendar feature which allows users to add events and
configure whether or not they repeat at various frequencies (i.e, daily,
weekly, monthly, Sat/Sun, etc.). What I'm looking for is some C# code that will calculate a date of "Every Other Week" based on a starting date and
ending date.

Example: If a user enters an event dated October 3, 2005 and would like it
to repeat every other week until October 31, 2005, the code will return the following dates:

October 3, 2005
October 17, 2005
October 31, 2005

Any help would be greatly appreciated.

Rob

Nov 17 '05 #2
It's a bit more complex than that Ab. In order to determine how many dates
there are representing "Every other Week" starting on October 3, 2005 and
ending on October 31, 2005, you first have to figure out how many entries
there should be. In this case, there should be 3. A "for loop" is required
to loop through Oct 3 to Oct 31 and return either the number of days or
weeks. Example, the code below will insert the correct number of entries for
an event that repeated every day from Oct 3 to Oct 31 (intNumberofDays is
determined using TimeSpan):

if (EventFrequency == "Day")
{
for(int i = 0; i <= intNumberofDays; i++)
{
string strInsert = "INSERT INTO Events " +
"(EventStartDateTime, " +
"EventEndDateTime) VALUES " +
"('" + dtEventStartDateTime.AddDays(i) + "', " +
"'" + dtEventEndDateTime.AddDays(i) + "') ";

objConnection = new SqlConnection(dbPath);
objCommand = new SqlCommand(strInsert,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
}
}

The problem I'm running into is substituting "intNumberofDays" with
"intNumberofWeeks" will return 5 weeks, where I really need it to return 3
weeks. "i" will then be 3 which I can plug into a revised SQL insert
statement and use "dtEventStartDateTime.AddWeeks(i)" to populate my database
with the 3 dates representing "Every Other Week" (Oct 3, Oct, 17, Oct 31).
Simply subtracting 2 weeks won't work since the duration of events entered
by users can change from entry to entry.
"Abubakar" <ab*******@gmail.com> wrote in message
news:Ot**************@TK2MSFTNGP12.phx.gbl...
I think its as simple as adding number of days to the datetime type as:

DateTime dt = new DateTime(2005, 10, 3);
dt=dt.AddDays(14);

now dt has 17th oct. You keep on adding days like that until the the end
date comes, like in a loop.

Hope that helps.

Ab.
http://joehacker.blogspot.com
"Rob Johnson" <rj**********@comcast.net> wrote in message
news:nN********************@comcast.com...
I have an ASP.Net calendar feature which allows users to add events and
configure whether or not they repeat at various frequencies (i.e, daily,
weekly, monthly, Sat/Sun, etc.). What I'm looking for is some C# code

that
will calculate a date of "Every Other Week" based on a starting date and
ending date.

Example: If a user enters an event dated October 3, 2005 and would like it to repeat every other week until October 31, 2005, the code will return

the
following dates:

October 3, 2005
October 17, 2005
October 31, 2005

Any help would be greatly appreciated.

Rob


Nov 17 '05 #3
I think I've resolved my issue. The key was to divide "intNumberofWeeks" by
2. The following code seems to work fine...so far:

for(int i = 0; i <= intNumberofWeeks / 2; i++)
{
string strInsert = "INSERT INTO Events " +
"(EventStartDateTime, " +
"EventEndDateTime) VALUES " +
"('" + dtEventStartDateTime.AddDays(i * 14) + "', " +
"'" + dtEventEndDateTime.AddDays(i * 14) + "') ";

objConnection = new SqlConnection(dbPath);
objCommand = new SqlCommand(strInsert,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
}

"Rob Johnson" <rj**********@comcast.net> wrote in message
news:M-********************@comcast.com...
It's a bit more complex than that Ab. In order to determine how many dates there are representing "Every other Week" starting on October 3, 2005 and
ending on October 31, 2005, you first have to figure out how many entries
there should be. In this case, there should be 3. A "for loop" is required to loop through Oct 3 to Oct 31 and return either the number of days or
weeks. Example, the code below will insert the correct number of entries for an event that repeated every day from Oct 3 to Oct 31 (intNumberofDays is
determined using TimeSpan):

if (EventFrequency == "Day")
{
for(int i = 0; i <= intNumberofDays; i++)
{
string strInsert = "INSERT INTO Events " +
"(EventStartDateTime, " +
"EventEndDateTime) VALUES " +
"('" + dtEventStartDateTime.AddDays(i) + "', " +
"'" + dtEventEndDateTime.AddDays(i) + "') ";

objConnection = new SqlConnection(dbPath);
objCommand = new SqlCommand(strInsert,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
}
}

The problem I'm running into is substituting "intNumberofDays" with
"intNumberofWeeks" will return 5 weeks, where I really need it to return 3
weeks. "i" will then be 3 which I can plug into a revised SQL insert
statement and use "dtEventStartDateTime.AddWeeks(i)" to populate my database with the 3 dates representing "Every Other Week" (Oct 3, Oct, 17, Oct 31).
Simply subtracting 2 weeks won't work since the duration of events entered
by users can change from entry to entry.
"Abubakar" <ab*******@gmail.com> wrote in message
news:Ot**************@TK2MSFTNGP12.phx.gbl...
I think its as simple as adding number of days to the datetime type as:

DateTime dt = new DateTime(2005, 10, 3);
dt=dt.AddDays(14);

now dt has 17th oct. You keep on adding days like that until the the end
date comes, like in a loop.

Hope that helps.

Ab.
http://joehacker.blogspot.com
"Rob Johnson" <rj**********@comcast.net> wrote in message
news:nN********************@comcast.com...
I have an ASP.Net calendar feature which allows users to add events and configure whether or not they repeat at various frequencies (i.e, daily, weekly, monthly, Sat/Sun, etc.). What I'm looking for is some C# code that
will calculate a date of "Every Other Week" based on a starting date and ending date.

Example: If a user enters an event dated October 3, 2005 and would
like it to repeat every other week until October 31, 2005, the code will
return the
following dates:

October 3, 2005
October 17, 2005
October 31, 2005

Any help would be greatly appreciated.

Rob



Nov 17 '05 #4
Sal
There's no need to care about the number of inserts you're doing at the time
you're calculating it (unless of course you have this constraint).

DateTime dt = startDate;
int frequency = 14;
int insertCount = 0;
int maxInserts = 100; //may not be necessary for you...

//begin tran

while( dt <= endDate && insertCount < maxInserts )
{
//insert dt into your date table

dt.AddDays( frequency );
insertCount++;
}

if( insertCount >= maxInserts )
{rollback tran}
else
{commit tran}

"Rob Johnson" <rj**********@comcast.net> wrote in message
news:_Z********************@comcast.com...
I think I've resolved my issue. The key was to divide "intNumberofWeeks"
by
2. The following code seems to work fine...so far:

for(int i = 0; i <= intNumberofWeeks / 2; i++)
{
string strInsert = "INSERT INTO Events " +
"(EventStartDateTime, " +
"EventEndDateTime) VALUES " +
"('" + dtEventStartDateTime.AddDays(i * 14) + "', " +
"'" + dtEventEndDateTime.AddDays(i * 14) + "') ";

objConnection = new SqlConnection(dbPath);
objCommand = new SqlCommand(strInsert,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
}

"Rob Johnson" <rj**********@comcast.net> wrote in message
news:M-********************@comcast.com...
It's a bit more complex than that Ab. In order to determine how many

dates
there are representing "Every other Week" starting on October 3, 2005 and
ending on October 31, 2005, you first have to figure out how many entries
there should be. In this case, there should be 3. A "for loop" is

required
to loop through Oct 3 to Oct 31 and return either the number of days or
weeks. Example, the code below will insert the correct number of entries

for
an event that repeated every day from Oct 3 to Oct 31 (intNumberofDays is
determined using TimeSpan):

if (EventFrequency == "Day")
{
for(int i = 0; i <= intNumberofDays; i++)
{
string strInsert = "INSERT INTO Events " +
"(EventStartDateTime, " +
"EventEndDateTime) VALUES " +
"('" + dtEventStartDateTime.AddDays(i) + "', " +
"'" + dtEventEndDateTime.AddDays(i) + "') ";

objConnection = new SqlConnection(dbPath);
objCommand = new SqlCommand(strInsert,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
}
}

The problem I'm running into is substituting "intNumberofDays" with
"intNumberofWeeks" will return 5 weeks, where I really need it to return
3
weeks. "i" will then be 3 which I can plug into a revised SQL insert
statement and use "dtEventStartDateTime.AddWeeks(i)" to populate my

database
with the 3 dates representing "Every Other Week" (Oct 3, Oct, 17, Oct
31).
Simply subtracting 2 weeks won't work since the duration of events
entered
by users can change from entry to entry.
"Abubakar" <ab*******@gmail.com> wrote in message
news:Ot**************@TK2MSFTNGP12.phx.gbl...
> I think its as simple as adding number of days to the datetime type
> as:
>
> DateTime dt = new DateTime(2005, 10, 3);
> dt=dt.AddDays(14);
>
> now dt has 17th oct. You keep on adding days like that until the the
> end
> date comes, like in a loop.
>
> Hope that helps.
>
> Ab.
> http://joehacker.blogspot.com
>
>
> "Rob Johnson" <rj**********@comcast.net> wrote in message
> news:nN********************@comcast.com...
> > I have an ASP.Net calendar feature which allows users to add events and > > configure whether or not they repeat at various frequencies (i.e, daily, > > weekly, monthly, Sat/Sun, etc.). What I'm looking for is some C#
> > code
> that
> > will calculate a date of "Every Other Week" based on a starting date and > > ending date.
> >
> > Example: If a user enters an event dated October 3, 2005 and would like
it
> > to repeat every other week until October 31, 2005, the code will

return > the
> > following dates:
> >
> > October 3, 2005
> > October 17, 2005
> > October 31, 2005
> >
> > Any help would be greatly appreciated.
> >
> > Rob
> >
> >
>
>



Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Wm | last post: by
2 posts views Thread by Pete..... | last post: by
7 posts views Thread by Steve | last post: by
8 posts views Thread by Shyguy | last post: by
2 posts views Thread by Kajsa Anka | last post: by
1 post views Thread by swethak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.