简体   繁体   English

无法将值从字符串转换为datetime

[英]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文件中编写的...它给出了转换为日期时间等错误....

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

in cs file i wrote... 在cs文件中我写了...

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

it gives following error.. 它给出了以下错误..

String was not recognized as a valid Date Time. 字符串未被识别为有效的日期时间。

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
   }

这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM