Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting error in b.Dob = DateTime.Parse(txtdob.Text);
Posted
Comments
Kenneth Haugland 26-Oct-13 6:21am    
With what? I cant tell whats wrong here, so whats in b.Dob?
sanjaysgh 26-Oct-13 6:24am    
b.Dob = DateTime.Parse(txtdob.Text);
when i debug program i am getting exception that String was not recognized as a valid DateTime.
Kenneth Haugland 26-Oct-13 6:26am    
I assume that b is a class, and Dob is a property in the class, so what does the class b look like?
Kenneth Haugland 26-Oct-13 6:26am    
Use TryParse instead....
sanjaysgh 26-Oct-13 6:29am    
getting error while using tryparse asking for 2 arguments

1 solution

IF you look in the documentation[^] there is an example that would solve your problem.

The code:
C#
CultureInfo enUS = new CultureInfo("en-US"); 
string dateString;
DateTime dateValue;

// Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM";
if (DateTime.TryParseExact(dateString, "g", enUS, 
                           DateTimeStyles.None, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

// Allow a leading space in the date string. 
if (DateTime.TryParseExact(dateString, "g", enUS, 
                           DateTimeStyles.AllowLeadingWhite, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

// Use custom formats with M and MM.
dateString = "5/01/2009 09:00";
if (DateTime.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS, 
                           DateTimeStyles.None, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

// Allow a leading space in the date string. 
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, 
                        DateTimeStyles.None, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

// Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00"; 
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, 
                        DateTimeStyles.None, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

// Allow a leading space in the date string. 
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, 
                        DateTimeStyles.AdjustToUniversal, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

// Parse a string represengting UTC.
dateString = "2008-06-11T16:11:20.0904778Z";
if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, 
                               DateTimeStyles.None, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, 
                           DateTimeStyles.RoundtripKind, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

// The example displays the following output: 
//    ' 5/01/2009 8:30 AM' is not in an acceptable format. 
//    Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified). 
//    Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified). 
//    '5/01/2009 09:00' is not in an acceptable format. 
//    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local). 
//    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc). 
//    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local). 
//    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
 
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