Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the code is given below


C#
if (date != "" && date != null)
           {
               System.Globalization.DateTimeFormatInfo info = new System.Globalization.DateTimeFormatInfo();
               info.ShortDatePattern = "MM/dd/yyyy"
               DateTime ddate = DateTime.ParseExact(date, "dd/MM/yyyy", null);//here the error occures "string was not recoganized as a valid datetime"
               Userdir = (from u in Userdir where u.RegisteredDate >= ddate select u).OrderByDescending(c => c.RegisteredDate).ToList();
           }


thanks in advance
Posted
Updated 30-Nov-15 19:57pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Dec-15 0:34am    
Well, it just depends on date; one string can parse and another one not. You did not show any, so what are we discussing? Just check up everything thoroughly, that's all.
—SA
[no name] 1-Dec-15 0:45am    
The value in date variable is not correct date format. So when compiler tries to convert it to date it is throwing exception. In order to resolve problem please use correct date format like: "MM/dd/yyyy" -> "05/25/2015"
George Jonsson 1-Dec-15 3:02am    
It would help if you provided an example of the string you are trying to parse.
string date = ????

C#
info.ShortDatePattern = "MM/dd/yyyy"
DateTime ddate = DateTime.ParseExact(date, "MM/dd/yyyy", null);
 
Share this answer
 
v2
Use DateTime.ParseExact() method to parse string as in daterime format, I have written an article on it you can check on below link
http://prasaddotnettricks.blogspot.in/2014/05/convert-string-to-datetime-object.html[^]
 
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