Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parse date time string with ascii character to datetime

I have datetime data getting in XML like this

<Start_Date>1/10/2023 5&#58;00&#58;00 PM</Start_Date>

How to convert this to C# datetime.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

            string dateString_string = "1/10/2023 5&amp;#58;00&amp;#58;00 PM";
            CultureInfo culture = new CultureInfo("en-US");
            CultureInfo provider = CultureInfo.InvariantCulture;
            
            var tt = Convert.ToDateTime(dateString_string, culture);
            var tt1 = DateTime.Parse(dateString_string);

            DateTime dateTime10 = DateTime.ParseExact(dateString_string, "mm/dd/yyyy hh:mm:ss T", provider);

            DateTime dateTime12;
            bool isSuccess2 = DateTime.TryParse(dateString_string, out dateTime12);


            DateTime dateTime14;
            bool isSuccess6 = DateTime.TryParseExact(dateString_string, new string[] { "mm/dd/yyyy H&amp;#58;mm&amp;#58;ss tt"}, provider, DateTimeStyles.None, out dateTime14);

I try all this a no luck ..
any suggestion ?

>Solution :

You should first decode your string and then do the parsing.

string dateString = "1/10/2023 5&#58;00&#58;00 PM";
string decodedDate = WebUtility.HtmlDecode(dateString);
bool isSuccess = DateTime.TryParse(decodedDate, out DateTime parsedDate);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading