-1

I am retrieving the DateTime from a db which is coming through in the Dropdown list as the full date and time. What i need is just the month from that DateTime in word format in that drop down list.

As below:

[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 - This question is different. I am trying to do this as a new listItem. I understand how to convert DateTime int. I would love you to find a duplicate answer to that! :)

  • 2
    And what is the problem? – L.B Jun 24 '14 at 18:20
  • @L.B, the issue is not getting the Month name, instead the issue is `thisDate.Month.ToString("MMMM")`, trying to convert `int` value using `DateTime` format – Habib Jun 24 '14 at 18:23

1 Answers1

1

The problem is

thisDate.Month.ToString("MMMM")

DateTime.Month is of type int, if you are trying to add it as a value then simply use

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

If you want value to be Month Name then use:

thisDate.ToString("MMMM")

instead of passing a parameter to ToString, You will end up with all values set to MMMM

Habib
  • 219,104
  • 29
  • 407
  • 436