2013-01-18 58 views
0

嗨即时尝试将我在MSSQL的int日期时间转换为月名。 Im来自丹麦(DK),代码是。解析日期时间到monthname

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

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

但我想我错过了一些东西。

感谢

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

+0

什么是您的数据的格式转换? –

+0

此外,如果你已经做了TryParse,你可以做'KeyWord = dateTime.ToLongDateString();' – ryadavilli

回答

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)); 
0
Select DateName(Month, getdate()) as Month_Name 

将在MSSQL

+0

我试图从日期时间转换 – user1989680

0

月产名称可以自己枚举月

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

然后

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

你并不需要再次转换为日期时间,的TryParse会给你日期时间

+0

谢谢你。但doesent工作:(其搜索功能,我希望能够,在月份名称而不是月份搜索 – user1989680

+0

而不是说'但是请工作',请尝试让您的问题更清楚并示例! –

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. 
     } 
+0

谢谢你的家伙。但doesent的工作:(其搜索功能,我希望能够,在月份名称而不是monthnumber搜索。 – user1989680

+0

你可以给这个例子输入和输出吗? –