Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .NET

USE of DateTime.ParseExact Method

0.00/5 (No votes)
18 Mar 2010CPOL 1  
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Globalization;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { //String s = 17-03-2010; //DateTime...
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //String s = "17-03-2010";
            //DateTime dt = DateTime.ParseExact(s, "DD-MM-YYYY", CultureInfo.InvariantCulture);
            //Console.WriteLine(dt.ToString());
            //string dateString = "Mon 16 Jun 8:30 AM 2008"; // Modified from MSDN
            //string format = "ddd dd MMM h:mm tt yyyy";
            DateTime dateTime;
            string dateString = "18MAY_03_2010"; // Modified from MSDN
            string format = "ddMMM_MM_yyyy";
            try
            {
                dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
            }
            catch (FormatException fe)
            {
                Console.WriteLine("Incorrect Date Format.");
                Console.ReadKey();
                return;
            }
            Console.WriteLine(dateTime);
            Console.ReadKey();
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)