16
votes

Guys i am unable to convert datetime to "dd-MMM-yyyy" format. My code is given below:

TreeNode tn = new TreeNode(dr["pBillDate"].ToString());       // Here i am getting both date and time.

I need to convert it as "20-Mar-2013".

Please help me. Thanks.

5
If its working (as mentioned below) then you might want to accept the answer that provided you the solution.Mark Vickery
old question but I search for parse a "dd MMM yyyy" string to date, and on stackoverflow.com/questions/5330909/… post, a solution says that have to use DateTime.ParseExact to parse any date string telling what format like to parse, then can convert as the answer on this post says with tostring("dd mm etc....") again to another string formatFabianSilva

5 Answers

52
votes

Try this code:

string formattedDate = YourDate.ToString("dd MMM yyyy");

It will format to like:

12 Nov 2012
8
votes

The following couple examples should work:

DateTime dt = Convert.ToDateTime(dr["pBillDate"]);

TreeNode tn = new TreeNode(String.Format("{0:dd-MMM-yyyy}", dt));

or

DateTime dt = Convert.ToDateTime(dr["pBillDate"]);

TreeNode tn = new TreeNode(dt.ToString("dd-MMM-yyyy"));
6
votes
DateTime StartDate = Convert.ToDateTime(datepicker.Text);
string Date = StartDate.ToString("dd-MMM-yyyy");
2
votes

Find below code:

DateTime todayDay = DateTime.Today.ToString();

It will provide you with dd-MMM-yyyy format.

1
votes

try Following

    TreeNode tn = new TreeNode(dr["pBillDate"].ToShortDateString()); 

it can convert DateTime to Date

Thanks,