DateTimePicker, get first and last day of the month [closed]

2

How do I get the first and last day of the previous month, to fill a% on screen with%?

Ex:

Data Inicial 01/07/2015
Data Final   31/07/2015
    
asked by anonymous 12.08.2015 / 15:12

2 answers

2

The question is not very clear but it should look something like this:

var data = picker.Value; //pega a data que está no controle
var mesAnterior = data.AddMonths(-1);
var primeiroDia = new DateTime(mesAnterior.Year, mesAnterior.Month, 1); 
var ultimoDia =  new DateTime(mesAnterior.Year, mesAnterior.Month,
        DateTime.DaysInMonth(mesAnterior.Year, mesAnterior.Month));

There are numerous other formulas to do this. Each may have one advantage over another but will generally make little or no difference in simple things. I did a test below using extension methods, so you can use it as if it were part of the type.

See working on dotNetFiddle .

    
12.08.2015 / 16:26
0

There is an answer #

See if this helps you too:

DateTime dtUltimoDiaMes = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);

Remember that. The last day of the previous month is primeiro_dia_mês_corrente menos_um_dia(AddDays(-1)) . This is the formula.

    
12.08.2015 / 15:34