Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends..I m trying to convert my date in mm/dd/yyyy formate..i writtenn following code but code is not working...i dont know what is the problem.please help me.Thanks.
C#
>

 string fromdate = Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text.ToString();
 DateTime dt1 = DateTime.ParseExact(fromdate, "MM/dd/yyyy", null);
Posted
Comments
Andy411 19-Nov-12 2:24am    
Do I understand you right: You have a string like 11/30/2012 and you want to put it in a variable of type DateTime?

A simple ToString with format should do the job Aysha.

C#
dt1.ToString("MM/dd/yyyy");


hope that helps. If it does, mark the answer.

Thanks
Milind
 
Share this answer
 
You can't store date as your require format in datetime variable;

you can do:

C#
string fromdate = Convert.ToDateTime(Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text).ToString("MM/dd/yyyy");


Or
C#
string fromdate = Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text.ToString();
string dt1 = Convert.ToDateTime(fromdate).ToString("MM/dd/yyyy");
 
Share this answer
 
You did not add a culture. If you set the provider to null, the parser method uses the current culture.

Did you try this?
C#
string dateString = "11/30/2012";
CultureInfo ci = CultureInfo.InvariantCulture;

DateTime myDate = DateTime.ParseExact(dateString, "MM/dd/yyyy", ci);


Or with your code:
C#
CultureInfo ci = CultureInfo.InvariantCulture;
string fromdate = Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text.ToString();
DateTime dt1 = DateTime.ParseExact(fromdate, "MM/dd/yyyy", ci);


On my system, current culture is de-DE and with format = null I get a exception with this date format, but whenn I set the invariant culture, it works an parses the date correct.
 
Share this answer
 
v2
please try it.

C#
DateTime date = new DateTime(2011, 2, 19);
string formatted = date.ToString("dd/M/yyyy");



or you can try this also..
C#
public static string GetDateFromDateTime(DateTime datevalue){
    return datevalue.ToShortDateString(); 

//Here is a method, that takes datetime(format:01-01-2012 12:00:00) and returns //string(format: 01-01-2012) 
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900