简体   繁体   English

c#-文本框日期值显示为01/01/1900

[英]c# - Textbox date value shows as 01/01/1900

I have a textbox as per below that is meant to capture the date: 我有一个下面的文本框,用于捕获日期:

<input name="datereceived" type="date" class="FormDateValues" id="datereceived"  value="" runat="server" />

However, when I try and retreive the value in the code behind, I get a value of 01/01/1900. 但是,当我尝试检索后面代码中的值时,得到的值是01/01/1900。

DateTime InvoiceDate = DateTime.Parse(datereceived.Value);

Can someone please tell me why is this returning a blank date? 有人可以告诉我为什么返回空白日期吗?

Thanks 谢谢

Do you have any format for your datereceived variable? 您的datereceived变量是否有任何格式? If so, you can try this: 如果是这样,您可以尝试以下操作:

DateTime.ParseExact(datereceived.Value, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);

You can specify the exact format of your date via the MM/dd/yyyy format specifiers, also, try to look at other possible parsing technique using TryParseExact() to avoid exceptions. 您可以通过MM/dd/yyyy格式说明符指定日期的确切格式,也可以尝试使用TryParseExact()查看其他可能的解析技术,以避免出现异常。

String was not recognized as a valid DateTime error will occur due to following reason : 由于以下原因,不会将字符串识别为有效的DateTime错误:

1. If input text/ value is empty: If value is empty in input field then your conversion from null to date time will cause this. 1.如果输入文本/值是空的:如果输入字段中的值是空的,则您从null到日期时间的转换将导致此情况。

2.If input text/value is in different format that yo want to going to convert. 2.如果输入的文本/值是您要转换的其他格式。 Let suppose your input field is in "dd-MM-yyyy" and you are Parse it to "dd/MM/yyyy" then it will be through error. 假设您的输入字段在“ dd-MM-yyyy”中,并且您将其解析为“ dd / MM / yyyy”,那么它将是错误的。

So please check your format and convert it in other ways. 因此,请检查您的格式并以其他方式进行转换。

May this help you to figure out your error. 也许这可以帮助您找出错误。

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

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