c# -格式化日期时间到月份

本文关键字:时间 日期 格式化 | 更新日期: 2023-09-27 18:10:03

我正在从下拉列表中作为完整日期和时间的数据库中检索DateTime。我需要的只是从那个日期时间在word格式下拉列表中的月份。

:

[System.Web.Services.WebMethod]
public static string OnChainChange(string ChainId)
{
    DataTable table = DBHelper.GetMonthList(ChainId.Trim());
    string result = string.Empty;
    foreach (DataRow dr in table.Rows)
    {
        DateTime thisDate = Convert.ToDateTime(dr["FromDate"].ToString());
        result += new ListItem(thisDate.ToString("MMMM"), thisDate.Month.ToString("MMMM"));
        //result += dr["FromDate"].ToString() + ";";
    }
    return result;
}

LB -这个问题不同。我试着把它作为一个新的列表项。我知道如何转换DateTime int。我希望你能找到一个重复的答案!:)

c# -格式化日期时间到月份

问题是

thisDate.Month.ToString("MMMM")

DateTime.Month的类型为int,如果您想将其添加为值,则只需使用

thisDate.Month.ToString() //without passing in any parameter

如果您希望value为Month Name,则使用:

thisDate.ToString("MMMM")

而不是传递一个参数给ToString,你将结束所有的值设置为MMMM