Click here to Skip to main content
Sign Up to vote bad
good
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:05

Comments
Andy411 - 19 Nov '12 - 2:24
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?

4 solutions

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  
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  
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  
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 273
1 Mahesh Bailwal 230
2 Ron Beyer 220
3 Aarti Meswania 195
4 Rohan Leuva 170
0 Sergey Alexandrovich Kryukov 8,548
1 OriginalGriff 6,819
2 CPallini 3,648
3 Rohan Leuva 2,933
4 Maciej Los 2,288


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid