我有以下列方式,但在选择12月份标记日期错误时,12月份没有给我#/ p>
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;
}
格拉西亚斯
答案 0 :(得分:0)
如果我正确地理解了这一点,那么你真正做的就是花一个月的时间,加上1,所以它实际上是下个月。然后你从中减去一天,这样它就是上个月的最后一天。最好是给它一个实际的日期,而不是做一个圆形的方式。
替换
DateTime fechaFinal = new DateTime(Convert.ToInt32(ddlAñoFinal.SelectedValue),
Convert.ToInt32(ddlMesFinal.SelectedValue) +1,
01).AddDays(-1);
与
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);