我有它以下列方式,但十二月份選擇一個月的時候日期如何獲得每個月的最後一天?
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;
}
十二月標記錯誤的格拉西亞斯
有什麼錯誤? –
[DaysInMonth函數](https://msdn.microsoft.com/en-us/library/system.datetime.daysinmonth.aspx) – MethodMan
可能出現[如何查找每月的最後一天?](http: //stackoverflow.com/questions/4078954/how-do-you-find-the-last-day-of-the-month) – MethodMan