Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all dears !
i have a string that contain DateTime value like :
C#
Tue Jun 12 00:17:44 2012

now if i want to parse by
C#
DateTime.Parse(dateTime);
occure an error :
String was not recognized as a valid DateTime.

how i solve ?
Posted
Comments
Perić Željko 11-Jun-12 16:30pm    
Hello,
It is obvious that string was not recognised as valid DateTime format. So check this links for solution :

http://msdn.microsoft.com/en-us/library/system.datetime.aspx
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

Have a look at this: DateTime.TryParse[^] method and use it.

DateTime.TryParse Method (String, IFormatProvider, DateTimeStyles, DateTime%)

The most important is:
Specific valid formats for date and time elements, as well as the names and symbols used in dates and times, are defined by the provider parameter, which can be any of the following:

A CultureInfo object that represents the culture whose formatting is used in the s parameter. The DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property defines the formatting used in s.

A DateTimeFormatInfo object that defines the formatting used in s.

A custom IFormatProvider implementation. Its IFormatProvider.GetFormat method returns a DateTimeFormatInfo object that defines the formatting used in s.

If provider is null, the current culture is used.
 
Share this answer
 
Comments
Wonde Tadesse 11-Jun-12 20:33pm    
5+
Maciej Los 12-Jun-12 9:19am    
Thank you ;)
VJ Reddy 12-Jun-12 4:18am    
Good answer. 5!
Maciej Los 12-Jun-12 9:19am    
Thank you ;)
Sandeep Mewara 14-Jun-12 16:34pm    
Good answer. 5!
Better try like below.

DateTime ExDate = DateTime.ParseExact(txtExpiryDate.Text, "dd/MM/yyyy", null);

(or)


DateTime ExDate = Convert.ToDateTime("txtExDate".Text);
 
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