Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have this datetime format : 12/22/2014 01:31:38 AM

and i need to parse it DateTime.Parse(...), but when i try to do it it give me an error :

String was not recognized as a valid DateTime.

what is the solution to parse it and convert it to datetime format
Posted
Comments
Sergey Alexandrovich Kryukov 22-Dec-14 18:02pm    
What's wrong with just reading original MSDN documentation on DateTime formatting? Everything is clearly explained.
—SA

Because this is not a valid format for the current culture. Please see all the System.DateTime.Parse, TryParse, ParseExact and TryParseExact methods:
http://msdn.microsoft.com/en-us/library/system.datetime.parse%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.datetime.tryparse%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.datetime.parseexact%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact%28v=vs.110%29.aspx[^].

You will find all the way to setup format options, either using format string or System.Globalization.CultureInfo (through System.IFormatProvider implemented by the class CultureInfo; you can also have a custom implementation of this interface). For string formats, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^].

See also:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.iformatprovider%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/91hfhz89%28v=vs.110%29.aspx[^].

That's all. Get familiar with all that, and you will be able to parse DateTime any thinkable format.

—SA
 
Share this answer
 
Comments
ENG.Samy Sammour 22-Dec-14 18:08pm    
i know about datetime and read the formula ... but i read this information from a text and i need it to be able to parse because i cannot change the source of the formula
Sergey Alexandrovich Kryukov 22-Dec-14 18:33pm    
Reading is not enough; you should also understand. The problem is way too trivial. What have you tried so far?
—SA
I found the solution

the write format :
dd/MM/yyyy hh:mm:ss tt

12/22/2014 01:31:38 AM


so i do:

C#
string[] temp = "12/22/2014 01:31:38 AM".Split('/');
date = DateTime.Parse(temp[1] + '/' + temp[0] + '/' + temp[2]);
 
Share this answer
 
This should work:

C#
var dateString = "12/22/2014 01:31:38 AM";
var dateTime = DateTime.Parse(dateString);


Cheers!
 
Share this answer
 
Comments
ENG.Samy Sammour 22-Dec-14 18:12pm    
no it did not

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