Get the friday and satureday night of each day

2133 views c#
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)
        {
            ArrivalDate = ArrivalDate.AddDays(1);
        }

        while (DepartureDate.DayOfWeek != DayOfWeek.Friday)
        {
            DepartureDate = DepartureDate.AddDays(1);
        }

answered question

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

1 Answer

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;          
    }

    date = date.AddDays(1);
}

posted this

Have an answer?

JD

Please login first before posting an answer.