Convert dd mm yyyy string to datetime

Create a function as shown below.
Your can use any one of the method give below inside the function, rest you can comment it out.
private DateTime FormatDate(string _Date)
{
        DateTime Dt;
            
        //Method 1
        System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
        dateInfo.ShortDatePattern = "dd/MM/yyyy";
        Dt = Convert.ToDateTime(_Date, dateInfo);
        //Method 2
        IFormatProvider mFomatter = new System.Globalization.CultureInfo("en-US");
        Dt = DateTime.ParseExact(_Date, "dd/MM/yyyy", mFomatter);
        //Method 3
        Dt = DateTime.ParseExact(_Date, "dd/MM/yyyy", null);
            
        return Dt;
}
To call the function use below code
string UrDate = "27/08/2008";
DateTime _obj = FormatDate(UrDate);

Reference: Muthukumar (http://nadarmuthukumar.blogspot.in)

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More