How to try parse exact date time value from a string with format string in C#

1 Answer

0 votes
using System;
using System.Globalization;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Wed 18 Jan 9:15 AM 2017"; 

            string format = "ddd dd MMM h:mm tt yyyy";

            DateTime dt;
            if (DateTime.TryParseExact(s, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
            {
                Console.WriteLine(dt);
            }
        }
    }
}

/*
run:
   
18/01/2017 09:15:00

*/

 



answered Jan 18, 2017 by avibootz

Related questions

1 answer 165 views
1 answer 125 views
1 answer 157 views
1 answer 148 views
1 answer 161 views
2 answers 181 views
1 answer 165 views
...