Search week number of the year through a date in C #

2

I'm writing a function that gets a string with a date (format dd/mm/aaaa ) and it should return a date in the same format 4 weeks later.

Example:

Entrada: 07/04/2016
saída: 05/05/2016

I read about it and thought of the following logic:

Read the date and make the corresponding week an integer, add 4, and reverse the process.

Follow the code below:

    //entrada 07/04/2016
    private string CalcularDataEntrega(string dataEntrada)
    {
        //regra: Data Geração + 4 Semanas;
        DateTime dataGen = DateTime.Parse(dataEntrada);
        Calendar cal = new GregorianCalendar();
        int semanaInicio = cal.GetWeekOfYear(dataGen); //erro
        int semanaFim = semanaInicio + 4;

        //retornar data com base na semana
        string dataSaida; //o que colocar?

        return dataSaida;
    }

In this Example of the documentation Microsoft has not been clear what exactly this function GetWeekOfYear returns (integer, string?).

    
asked by anonymous 07.04.2016 / 20:13

2 answers

2

Just do this:

DateTime.Parse(dataEntrada).AddDays(28).ToString()

See working on dotNetFiddle .

I am assuming that the date will be guaranteed in an appropriate format. If this is not guaranteed, there will be an exception. Ideally it would be good to handle this somehow, perhaps using a TryParse() . Avoid working with dates stored as string whenever possible.

    
07.04.2016 / 20:21
1

The function you are looking for is this same GetWeekOfYear and it will return an integer (Int32), as described on the page.

Return Value Type: System.Int32 A positive integer that represents the week of the year that includes the date in the time parameter.

Hugs,

    
07.04.2016 / 20:23