How to parse and display full date and time include milliseconds from string in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "15/8/2018 9:27:31.196 AM";

            try
            {
                DateTime dt = DateTime.Parse(s);

                Console.WriteLine("{0}", dt.ToString("dd/MM/yyyy hh:mm:ss.fff tt"));
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}


/*
run:
  
15/08/2018 09:27:31.196 AM
 
*/

 



answered Aug 15, 2018 by avibootz
0 votes
using System;
using System.Globalization;
using System.Threading;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "8/15/2018 9:27:31.196 AM";

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            try
            {
                DateTime dt = DateTime.Parse(s);

                Console.WriteLine("{0}", dt.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}


/*
run:
  
08/15/2018 09:27:31.196 AM

*/

 



answered Aug 15, 2018 by avibootz

Related questions

1 answer 119 views
1 answer 165 views
2 answers 182 views
1 answer 100 views
1 answer 99 views
1 answer 145 views
145 views asked Jan 18, 2017 by avibootz
...