Click here to Skip to main content
15,889,462 members
Articles / .NET
Alternative
Tip/Trick

USE of DateTime.ParseExact Method

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Mar 2010CPOL 7.5K   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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --