Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Counting the number of specific days in a year

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
crabb - 30 Oct 2007 21:39 GMT
I would like to know if anyone knows how to set up an app that would
count the number of specific days in a user defined year.  example,
how many Sundays, Mondays, Tuesdays, etc. in 2007 or a user defined
year, june 07 - june 08.  I really don't know where to start, but we
are working on financial data and i need this for calculations.

Thanks,
Eric
Teemu Keiski - 30 Oct 2007 22:17 GMT
Hi,

hardcore route is just instantiate DateTime with first date and then second
datetime with the end date. Then loop while adding AddDays(1) to the loop
date until you're at the end date. In the loop then just add one to every
day "counter"

It could be something like (not tested)

DateTime startDate = new DateTime(2007,1,1);
           DateTime loopDate = startDate;
           DateTime endDate = new DateTime(2007, 12, 31);

           int mondays = 0;

           do
           {
               if(loopDate.DayOfWeek == DayOfWeek.Monday)
                   mondays += 1;

                  loopDate=loopDate.AddDays(1);
           }
           while (loopDate <= endDate);

           //What does mondays variable contain at this point?

Signature

Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

>I would like to know if anyone knows how to set up an app that would
> count the number of specific days in a user defined year.  example,
[quoted text clipped - 4 lines]
> Thanks,
> Eric
John Timney (MVP) - 30 Oct 2007 22:44 GMT
You need a little bit of brute force, but its easy enough.

Quick and dirty works well for this

   DateTime fromDate = Convert.ToDateTime("1/01/2007");
   DateTime toDate = Convert.ToDateTime("09/01/2007");
   DateTime oNewDate;
   int dayCount = toDate.Subtract(fromDate).Days;
   Response.Write("Days " + dayCount + "<br>");
   int i = 0;
   int mon =0, tue=0, wed=0, thu=0, fri=0, sat=0, sun = 0;
   while (i < dayCount){
       oNewDate = fromDate.AddDays(i);
       if (oNewDate.DayOfWeek == DayOfWeek.Monday) {mon += 1;}
       if (oNewDate.DayOfWeek == DayOfWeek.Tuesday){ tue += 1;}
       if (oNewDate.DayOfWeek == DayOfWeek.Wednesday){wed += 1;}
       if (oNewDate.DayOfWeek == DayOfWeek.Thursday){thu += 1;}
       if (oNewDate.DayOfWeek == DayOfWeek.Friday){fri += 1;}
       if (oNewDate.DayOfWeek == DayOfWeek.Saturday){sat += 1;}
       if (oNewDate.DayOfWeek == DayOfWeek.Sunday){sun += 1;}
       i++;
   }
   Response.Write("There are " + mon.ToString() + " Mondays");
   Response.Write("There are " + sun.ToString() + " Sundays");

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog

>I would like to know if anyone knows how to set up an app that would
> count the number of specific days in a user defined year.  example,
[quoted text clipped - 4 lines]
> Thanks,
> Eric

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.