string to date conversion

There has been always a Problem in working DateTime variable in ASP.NET when you are working on different environments.

So I feel that why not make some short of solution for that. Here i came up with some result. May be there is better alternative.

Create one common class or if you have in your project than all you need to create is static method to convert string into DateTime with ‘MM/dd/yyyy’ Format.

public static DateTime ParseDateFormatToMMDDYYYY(string oDate)
        IFormatProvider fp = new System.Globalization.CultureInfo("en-US");
        return DateTime.ParseExact(oDate, "MM/dd/yyyy", fp);

Once you have created static method now all you have to do is to use it when ever necessary.

 DateTime search_date = Utilities.ParseDateFormatToMMDDYYYY(Request.QueryString["searchDate"].ToString());

Here the format for Date can be Controlled from Utility Class you can make changes as per your requirement.

Hope this will help !!!

Shout it
kick it on

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s