Date field 7 days after the current date in c#

Asked

Viewed 542 times

3

I have a field in my application call txt_dataemissão in the case and the current date, and I have another field txt_deadline that I need you to show me in this capo the date counted with 7 days after the date of issue, and disregard the weekends, and possible to do directly in the code in c#, if yes how can I do this?

1 answer

2


Just compare if the day is Saturday or Sunday until seven days are added up

 DateTime d1 = Convert.ToDateTime(txt_dataemissão.Text);   
 int dias = 0;
 while (dias < 7)
 {
    d1 = d1.AddDays(1);
    if (diaUtil(d1)) dias++;
 }
 txt_deadline.Text = d1.ToString();

the diaUtil() method will tell whether the day is Saturday or Sunday

    private bool diaUtil(DateTime x)
    {
        if (x.DayOfWeek == DayOfWeek.Sunday || x.DayOfWeek == DayOfWeek.Saturday)
            return false;
        else
            return true;
    }
  • And if the issue date is Saturday or Sunday?

  • 1

    Its function diaUltil could be simplified in: return x.DayOfWeek == DayOfWeek.Sunday || x.DayOfWeek == DayOfWeek.Saturday

  • @Paulohdsousa are 7 days forward and not counting from the same '7 days after the date of issue'

  • 1

    Thank you very much for the answer that was needed vlw.

  • 1

    I understood, but so weekends are out of the question on this occasion, but even so Thanks for everyone’s attention. Vlw

  • 1

    @Marconi to make more sense of the method, the best would be return !(x.DayOfWeek == DayOfWeek.Sunday || x.DayOfWeek == DayOfWeek.Saturday) I agree with you on being a better option, but so I thought it would be easier for AP to understand :)

  • 1

    @Leolonghi then I would exchange the order with which the Boleyans return, it makes more sense. if (x.DayOfWeek == DayOfWeek.Sunday || x.DayOfWeek == DayOfWeek.Saturday)&#xA; return true;... But it turned out good anyway :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.