Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
My system date format is 'dd.MM.yyyy'.
I want convert the string "15.05.2012" to DateTime.
When I convert this using Convert.ToDateTime("15.05.2012"), it is showing an error message 'String cannot be convert to.....'. But I could convert the string "05.15.2012" to DateTime.
How can I do this? pls help
Posted
Updated 29-May-12 0:28am
v2

You have basically two options for this.
DateTime.Parse() and DateTime.ParseExact().

The first is very forgiving in terms of syntax and will parse dates in many different formats. It is good for user input which may come in different formats.

ParseExact will allow you to specify the exact format of your date string to use for parsing. It is good to use this if your string is always in the same format. This way, you can easily detect any deviations from the expected data.

Please refer:
Convert a String to a DateTime [^]
DateTime.Parse Method (String)[^]
Convert.ToDateTime Method (String)[^]
Similar discussion:
Convert String to DateTime Object [^]
 
Share this answer
 
Comments
Maciej Los 29-May-12 8:19am    
Good answer, my 5!
Don't forget to use CultureInfo Class to get format of the date in local system.
Prasad_Kulkarni 29-May-12 9:15am    
Thank you Isomac!
5! for solution in comment ;)
Try This
string myDateTimeValue = "2/16/1992 12:15:12";
        DateTime myDateTime = DateTime.Parse(myDateTimeValue);

or

You can go threw this link

http://www.ezineasp.net/post/C-Convert-String-to-DateTime.aspx[^]

http://www.dotnetspider.com/forum/4879-How-convert-string-datetime-datetime-format-using-C.aspx[^]
 
Share this answer
 
v3
C#
DateTime dateTime = DateTime.ParseExact("15.05.2012", "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
 
Share this answer
 
Comments
VJ Reddy 31-May-12 19:58pm    
Good answer. 5!
The Solution 4 given by DaveyM69 using DateTime.ParseExact with InvariantCulture is very good.

I want to add that, it is better to use d.M.yyyy format instead of dd.MM.yyyy format as shown below
C#
DateTime dateTime = DateTime.ParseExact("5.5.2012", "d.M.yyyy", 
                            System.Globalization.CultureInfo.InvariantCulture);

because
dd.MM.yyyy will work if exactly two digits are given for date and month like 05.05.2012 but will throw error if one digit is given for date and / or month like 5.5.2012.

The d.M.yyyy format will work both for one digit and two digits given for the date and month. 05.05.2012 and 5.5.2012 both will be read without any error.
 
Share this answer
 
Comments
Espen Harlinn 1-Jun-12 4:15am    
Well answered :-D
VJ Reddy 1-Jun-12 5:50am    
Thank you, Espen :)
DaveyM69 1-Jun-12 14:53pm    
Good advice - I had never even thought of that :-)
VJ Reddy 1-Jun-12 19:37pm    
Thank you, Davey :)
Try DateTime.Parse

http://msdn.microsoft.com/en-US/library/1k1skd40%28v=vs.80%29.aspx[^]

C#
string date1 = "15.05.2012"; 
DateTime parsed_date= DateTime.Parse(date1);
Console.WriteLine(parsed_date.Year.ToString() + parsed_date.Month.ToString() + parsed_date.Day.ToString();


Regards
 
Share this answer
 
v3

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