# Get the friday and satureday night of each day

2137 views
9

The question is that I need to find each Saturday and Friday night and charge them 140 on Friday or Saturday. Do I use a while loop??

``````  DateTime ArrivalDate = DateTime.Parse(txtArrivalDate.Text);

DateTime DepartureDate = DateTime.Parse(txtDepartureDate.Text);

TimeSpan numberOfNights = DepartureDate.Subtract(ArrivalDate);

decimal nights = numberOfNights.Days;

txtNights.Text = nights.ToString();
//txtTotalPrice
decimal pricePerNight = 120.00m;
decimal totalNight = nights * pricePerNight;

txtTotalPrice.Text = "\$" + totalNight.ToString();
txtAvgPrice.Text = "\$" + pricePerNight.ToString();

// finds the fridays
decimal morePricePerNight = 140.00m;

int i = 0;
// while loop
while (ArrivalDate.DayOfWeek != DayOfWeek.Friday)
{
}

while (DepartureDate.DayOfWeek != DayOfWeek.Friday)
{
}
``````

Instead of a loop you could use a little math and just calculate the number of Fridays and Saturdays between the two dates.

Not an exact duplicate but closely related - stackoverflow.com/q/248273/416574

10

Why not to use a while loop?

I guess you can write the following code:

``````var date = DepartureDate;
while (date < ArrivalDate)
{
if (date.DayOfWeek == DayOfWeek.Friday || date.DayOfWeek == DayOfWeek.Saturday)
{
totalNight += 140.00m;
}

}
``````

posted this