Wilman Patel
Wilman Patel

Reputation: 81

Can't convert value to datetime from string

This is what I have written in .asp file.. it gives error like converting to date time....

<asp:TextBox ID="inputDate" runat="server" ClientIDMode="Static"
 CssClass="inputDate regitextbox" value="06/14/2008"></asp:TextBox>

in cs file i wrote...

  var date = inputDate.Text.Trim();
  var da = Convert.ToDateTime(date);

it gives following error..

String was not recognized as a valid Date Time.

Upvotes: 0

Views: 1015

Answers (2)

Amit Joki
Amit Joki

Reputation: 59232

Just do this:

var dateTime = DateTime.Parse(inputDate.Text.Trim());

Upvotes: 0

Jaroslav Kubacek
Jaroslav Kubacek

Reputation: 1447

Try

DateTime date;
if (DateTime.TryParseExact(inputDate.Text.Trim(), "M/dd/yyyy", enUS, DateTimeStyles.None, out date))
   {
    //Action to use date;
   }
else
   {
    //action to tell user that inputDate.Text is not date string as expected
   }

Upvotes: 2

Related Questions