Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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.
 
>
 
 string fromdate = Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text.ToString();
 DateTime dt1 = DateTime.ParseExact(fromdate, "MM/dd/yyyy", null);
Posted 18-Nov-12 20:05pm
Comments
Andy411 at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

A simple ToString with format should do the job Aysha.
 
dt1.ToString("MM/dd/yyyy");
 
hope that helps. If it does, mark the answer.
 
Thanks
Milind
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can't store date as your require format in datetime variable;
 
you can do:
 
string fromdate = Convert.ToDateTime(Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text).ToString("MM/dd/yyyy");
 
Or
string fromdate = Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text.ToString();
string dt1 = Convert.ToDateTime(fromdate).ToString("MM/dd/yyyy");
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You did not add a culture. If you set the provider to null, the parser method uses the current culture.
 
Did you try this?
string dateString = "11/30/2012";
CultureInfo ci = CultureInfo.InvariantCulture;
 
DateTime myDate = DateTime.ParseExact(dateString, "MM/dd/yyyy", ci);
 
 
Or with your code:
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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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

or you can try this also..
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) 
}
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100