0

嗨,我试图将我在 MSSQL 中的 int datetime 转换为 monthname。我来自丹麦(DK),代码是。

    DateTime dateTime;
    if (DateTime.TryParse(KeyWord, out dateTime))
    {
        KeyWord = Convert.ToDateTime(KeyWord).ToLongDateString();
    }

    objCMD.Parameters.AddWithValue("@keyword", "%" + KeyWord + "%");

但我认为我错过了一些东西。

谢谢

编辑:我无法让它工作。我正在从数据库中获取所有日期,并且我想在搜索功能中使用月份名称。我不确定当它具有搜索功能时是否需要做其他事情。

4

4 回答 4

0

您可以自己的月份枚举

enum Month
{
   January = 1,
   February = 2,
   ...
   ...
   Decmeber =12
}

然后

    DateTime dateTime;
    if (DateTime.TryParse(KeyWord, out dateTime))
    {
        KeyWord = ((Month)dateTime.Month).ToString();
    }

您无需再次转换为 DateTime,TryParse 将为您提供日期时间

于 2013-01-18T08:42:44.817 回答
0

试试这个 :

        DateTime dateTime;
        String KeyWord = "11/12/13"; // MM/dd/yy
        if (DateTime.TryParse(KeyWord, out dateTime))
        {
            KeyWord = Convert.ToDateTime(KeyWord).ToString("MMMM"); // Full month name
            KeyWord = Convert.ToDateTime(KeyWord).ToString("MMM"); // The abbreviated name of the month. 
        }
于 2013-01-18T08:47:57.113 回答
0
//Create localization object from http://stackoverflow.com/questions/3184121/get-month-name-from-month-number
System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.DateTimeFormatInfo();
//Initialize dateTime
DateTime.TryParse(KeyWord, out dateTime);
//Write Month name in KeyWord
KeyWord = string.Format("%{0}%", dtfi.GetMonthName(dateTime.Month));
于 2013-01-18T09:07:51.920 回答
0
string yourMonth= Date = DateTime.Now.Month.ToString();   
string selectedYear= DateTime.Now.Year.ToString();
ViewBag.Today = 
System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName
(Int32.Parse(yourMonth)) + selectedYear;

works fine for me hopefully it will help others

于 2019-01-24T07:10:20.193 回答