-6

I have it in the following way, but the month of December does not give it to me when selecting the month of December marks error of dates

 if (!Page.IsPostBack)
        {   DateTimeFormatInfo formatoFecha = CultureInfo.CurrentCulture.DateTimeFormat;
            TextInfo myTI = new CultureInfo("es-MX", false).TextInfo;
            for (int i = 1; i <= 12; i++)
            {
                ddlMesInicial.Items.Add(new ListItem(myTI.ToTitleCase(formatoFecha.GetMonthName(i)), i.ToString()));
                ddlMesFinal.Items.Add(new ListItem(myTI.ToTitleCase(formatoFecha.GetMonthName(i)), i.ToString()));
            }
            ddlMesInicial.SelectedValue = DateTime.Today.Month.ToString("MM");
            ddlMesFinal.SelectedValue = DateTime.Today.Month.ToString("MM");
            for (int i = 2013; i <= 2020; i++)
            { 
                ddlAñoInicial.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlAñoFinal.Items.Add(new ListItem(i.ToString()));                    
            }
            ddlAñoInicial.SelectedValue = DateTime.Today.Year.ToString("yyyy");
            ddlAñoFinal.SelectedValue = DateTime.Today.Year.ToString("yyyy");                

            FechaEntrega.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Today);

            SetFechasPeriodo();  
}

 private void SetFechasPeriodo()
    {
        DateTimeFormatInfo formatoFecha = CultureInfo.CurrentCulture.DateTimeFormat;
        TextInfo myTI = new CultureInfo("es-MX", false).TextInfo;
DateTime fechaInicial = new DateTime(Convert.ToInt32(ddlAñoInicial.SelectedValue), Convert.ToInt32(ddlMesInicial.SelectedValue), 01);
        DateTime fechaFinal = new DateTime(Convert.ToInt32(ddlAñoFinal.SelectedValue), Convert.ToInt32(ddlMesFinal.SelectedValue) +1, 01).AddDays(-1);
        
        string fdesde = fechaInicial.Day.ToString() + " de " + myTI.ToTitleCase(formatoFecha.GetMonthName(fechaInicial.Month)) + " de " + fechaInicial.Year.ToString();
        string fhasta = fechaFinal.Day.ToString() + " de " + myTI.ToTitleCase(formatoFecha.GetMonthName(fechaFinal.Month)) + " de " + fechaFinal.Year.ToString();

        lbFechasPeriodo.Text = "Periodo del " + fdesde + " al " + fhasta;
}
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Molitaa
  • 55
  • 6
  • 1
    What is the error? – Blake Thingstad Dec 14 '16 at 21:15
  • 2
    [DaysInMonth Function](https://msdn.microsoft.com/en-us/library/system.datetime.daysinmonth.aspx) – MethodMan Dec 14 '16 at 21:16
  • 4
    Possible duplicate of [How do you find the last day of the month?](http://stackoverflow.com/questions/4078954/how-do-you-find-the-last-day-of-the-month) – MethodMan Dec 14 '16 at 21:17
  • When I select the month of December it does not show me December 31 – Molitaa Dec 14 '16 at 21:17
  • 1
    @Molitaa yes, but is there an exception or a line number that isn't doing what you expect? I don't want to have interpret all of the code to try to figure out where it isn't working. – Blake Thingstad Dec 14 '16 at 21:20
  • Yes, when I write this Convert.ToInt32 (ddlMesFinal.SelectedValue) +1, 01) .AddDays (-1); It brings me the last day of each month but the one of December does not. Yes i write it Convert.ToInt32 (ddlMesFinal.SelectedValue), 01); Bring me the first day of every month I want the last day of every month – Molitaa Dec 14 '16 at 21:28
  • The last day of each month Not the number of days you have each month. – Molitaa Dec 14 '16 at 21:47

1 Answers1

0

If I'm understanding this correctly, what you're essentially doing is taking the month, adding 1 to it so it is actually the next month. Then you subtract a day from it so it's the last day of the previous month. It would just be better to give it the actual date instead of doing a round-about sort of way.

Replace

DateTime fechaFinal = new DateTime(Convert.ToInt32(ddlAñoFinal.SelectedValue), 
                                   Convert.ToInt32(ddlMesFinal.SelectedValue) +1, 
                                   01).AddDays(-1);

with

int añoFinal = Convert.ToInt32(ddlAñoFinal.SelectedValue);
int mesFinal = Convert.ToInt32(ddlMesFinal.SelectedValue);
int díaFinal = DateTime.DaysInMonth(añoFinal, mesFinal);
DateTime fechaFinal = new DateTime(añoFinal, mesFinal, díaFinal);
Blake Thingstad
  • 1,639
  • 2
  • 12
  • 19