get 25th day date from date today

Posted by Jopito under C# on 4/14/2014 | Points: 10 | Views : 1222 | Status : [Member] | Replies : 1
Hello friends,

I am working on a simple program to check the date in my code.I need to add 25days excluding weekends so as i can get the Date of the 25th day from my code.Is their anyone who can help me solve that?
Below is what am trying to check

public int NumberOfDays(DateTime startDate, int endDate)
{
int numberOfDays = 0;
DateTime end = startDate.AddDays(endDate);
while (startDate != end)
{
if (startDate.DayOfWeek != DayOfWeek.Saturday && startDate.DayOfWeek != DayOfWeek.Sunday)
{




numberOfDays++;



}
startDate = startDate.AddDays(1);

}
return numberOfDays;




}


Mark as answer if satisfied


Responses

Posted by: Vuyiswamb on: 4/15/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
First thing you need to have a function that get all days except weekends

public static List<DateTime> GetDatesExcludingWeekends(int year, int month)
{
return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
.Select(day => new DateTime(year, month, day))
.Where(dt => dt.DayOfWeek != DayOfWeek.Sunday &&
dt.DayOfWeek != DayOfWeek.Saturday)
.ToList();
}


and when you are done check for the 25th day of the month


List<DateTime> lstdates = GetDatesExcludingWeekends(2014, 1);

if (lstdates.Count >= 25)
{
for (int i = 0; i < lstdates.Count; i++)
{

if (i == 25)
{
MessageBox.Show("The 25th Day is: " + lstdates[i].ToString());

}


}
}
else
{
MessageBox.Show("The Month does not have days that are more than 25th after excluding Weekends");


}



i dont think you will get it 25th day if you exclude weekends , the highest might be 23

Good Luck


Thank you for posting at Dotnetfunda
[Administrator]

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response