473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6089
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Wm | last post by:
I have a client who needs a good web-based calendar for their website. What they would like is something that can display a monthly calendar online, with a display that's large enough that you can...
2
by: Pete..... | last post by:
Hi all. I'm trying to make a calendar for my webpage, python and html is the only programming languages that I know, is it possible to make such a calendar with pythong code and some html. ...
7
by: Steve | last post by:
Can someone recommend a way to display a one-month calendar view where the month can be selected and up to six events can be shown in any day of the calendar. The events and dates would be stored...
8
by: Shyguy | last post by:
Is it possible to create a calendar that shows previous input data and also allows for input of new data?
2
by: jodyblau | last post by:
I am trying use a Calendar Control 10.0 in one of my forms. (I am using access 2002) What I want to do is have the user click a button which makes the calendar visible. Then when the user...
2
by: Kajsa Anka | last post by:
Before I re-invent something I would like to ask if there exists some code that can be used for create the HTML code for a calendar which I then can include on a web page. The module in the...
6
by: hal | last post by:
Hello All, I got this code somewhere, so i'm not even sure if this is the best way to do this. Basically what i want is to display events in an asp.net calendar control which i'm able to do,...
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
1
by: swethak | last post by:
hi, i have a code to disply the calendar and add events to that. It works fine.But my requirement is to i have to disply a weekly and daily calendar.Any body plz suggest that what modifications i...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.