Mitul Birla
Mitul Birla

Reputation: 147

About DateTime in ASP.Net

I am trying to use the following code to assign date in my session:

Session["TransDate"] = Convert.ToDateTime(txtDate.Text).ToString("dd-MMM-yyyy");

Now it works fine in localhost, but it gives me error when I check it on server online. It gives me following error:

string was not in correct format.

What is the error?

Upvotes: 1

Views: 79

Answers (1)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

if your dateformat is dd-MM-yyyy (from your comments The date string is "25-01-2014").

Try This:

using System.Globalization;

DateTime dt;
if (DateTime.TryParseExact(txtDate.Text,"dd-MM-yyyy", CultureInfo.InvariantCulture,DateTimeStyles.None,out dt))
{
  string s= dt.ToString("dd-MMM-yyyy");
}
else
{
 //error message invalid date
}

Upvotes: 2

Related Questions