Return date x days left to arrive

Asked

Viewed 301 times

0

Scenario:

Ciclos
id 
name
endDate (datetime?)

Dice:

1, primeiro, 05/10/2017
2, segundo, 15/10/2017
3, terceiro, 20/10/2017
4, quarto, 30/10/2017

With Line and lambda, I would like to return the dates that are above the current date, but are below a certain amount of days informed by the user.

Example if today is day 01/10/2017

The user informs 20 days, he must return me the cycles 1, 2, 3 because these 3 records are up to 20 days to "win"

That one endDAte and a due date.

  • You already got the help?

  • Randrade’s head seems to suit me, I’ll finish the tests here, thanks for the concern @Francisco

1 answer

3


Let’s divide this into three steps.

First, you must get the current date. After that, you add that date with the amount of days informed by the user and finally, just add in your filter.

See the example below:

var listaCiclos = new List<Ciclos>(){
    new Ciclos() { id = 1, name = "primeiro", endDate = Convert.ToDateTime("05/10/2017") },
    new Ciclos() { id = 2, name = "segundo", endDate = Convert.ToDateTime("15/10/2017") },
    new Ciclos() { id = 3, name = "terceiro", endDate = Convert.ToDateTime("20/10/2017") },
    new Ciclos() { id = 4, name = "quarto", endDate = Convert.ToDateTime("30/10/2017") }
};


//Aqui eu adicionei a data de seu exemplo, mas você usaria Datetime.Now, para obter a data atual.
var dataAtual = Convert.ToDateTime("01/10/2017");

//Somamos a data atual mais a quantidade de dias digitado pelo usuário com o .AddDays()
var dataUsuario = dataAtual.AddDays(20);

//Filtramos a lista de acordo com os critérios informados
var listaFiltro = listaCiclos.Where(c => c.endDate > dataAtual && c.endDate < dataUsuario).ToList();

See a functional example in . Netfiddle

Browser other questions tagged

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