We help IT Professionals succeed at work.

Count days of the month

kensy11
kensy11 asked
on
Hello,

i have just made a simple program it ask for a date and it cheks for a leap year,

the second thing i  like this program to do is, to show the day number in the given date. so for example if the given date is  10/1/2010    i should say the date is its the 10th day day of the year ...

but i dont know how to do this anyone help me please??
i have upload the program.
thank you
ConsAppl1TI24.zip
Comment
Watch Question

Below Code will help
DateTime dt = DateTime.ParseExact(@"10\1\2010" , @"dd\MM\yyyy" , null);
int dayofYear = dt.DayOfYear;

Commented:
Hi,

You could use the DateTime.DayOfYear property.

http://msdn.microsoft.com/en-us/library/system.datetime.dayofyear.aspx

/peter

Author

Commented:
thanks , but how can i dot it manually i mean without the DateTime property

Author

Commented:
something like this??

ushort totaal;
        ushort m1 = 31;
        ushort m2 = 28;
        ushort m3 = 31;
        ushort m4 = 30;
        ushort m5 = 31;
        ushort m6 = 30;
        ushort m7 = 31;
        ushort m8 = 31;
        ushort m9 = 30;
        ushort m10 = 31;
        ushort m11 = 30;
        ushort m12 = 31;
        switch (Maand) {

          case 1:
                  totaal += m1;
                  break;
          case 2:
                  totaal +=(m1+m2);
                  if(ctrl.Jaar(jaar))
                     totaal +=1;
                  break;
          case 3:
                totaal +=(m1+m2+m3);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 4:
                totaal +=(m1+m2+m3+m4);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 5:
                totaal +=(m1+m2+m3+m4+m5);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 6:
                totaal +=(m1+m2+m3+m4+m5+m6);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 7:
                totaal +=(m1+m2+m3+m4+m5+m6+m7);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 8:
               totaal +=(1+2+3+4+m5+m6+m7+m8);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 9:
                totaal +=(1+2+3+4+5+6+7+8+9);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 10:
                totaal +=(1+2+3+4+5+6+7+8+9+10);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 11:
               totaal +=(1+2+3+4+5+6+7+8+9+10+11);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;
          case 12:
                totaal +=(1+2+3+4+5+6+7+8+9+10+11+12);
                if(ctrl.Jaar(jaar))
                    totaal +=1;
                break;


        }

Open in new window

Commented:
Why without DateTime? Even if you don't have it, I think it's easier to create one than do it yourself.

But if you want to do it yourself, you need an array of days for each month of the year, which you could sum up to previous month plus days of current month. And don't forget to add an extra day if it's a leap year and month > 2.
Commented:
I was thinking, something like:

int [] daysinmonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int days = 0;

for(int i = 0; i < yourmonth-1; i++) {
  days += daysinmonth[i];
}

days += yourdayofmonth;

if ((yourmonth > 2) && (youryear % 4 == 0) && ((youryear %100 != 0) || (youryear % 400 == 0))) {
  days++;
}

Open in new window