简体   繁体   English

无法将字符串识别为有效的DateTime(System.FormatException)?

[英]String was not recognized as a valid DateTime(System.FormatException)?

I am getting error as: 我收到以下错误消息:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code mscorlib.dll中发生类型'System.FormatException'的异常,但未在用户代码中处理

Additional information: String was not recognized as a valid DateTime. 附加信息:字符串未被识别为有效的DateTime。

This is my code : 这是我的代码:

if (txtRefDate.Text != "")
{
    string[] splitdate = txtRefDate.Text.Split('-');
    string newdate = splitdate[1] + "-" + splitdate[0] + "-" + splitdate[2];
    DateTime Compdate = Convert.ToDateTime(newdate);//On this line i'm getting error
    string date = Compdate.ToString("yyyy-MM-dd");
    obj.RefrenceDate = Convert.ToDateTime(date);
}

I am using CalenderExtender on my textbox . 我在textbox上使用CalenderExtender I tried Parse and ParseExact , but it is not working. 我尝试了ParseParseExact ,但是它不起作用。 What am I doing wrong here? 我在这里做错了什么?

DateTime Compdate;
if (!String.IsNullOrEmpty(txtRefDate.Text) && DateTime.TryParse(txtRefDate.Text, out Compdate))
{
    obj.RefrenceDate = Compdate.Date;
}

Also take a look at TryParseExact() , which allows you to supply more info about the expected format of your input. 还请看一下TryParseExact() ,它使您可以提供有关输入的预期格式的更多信息。

And if this fails, start logging the string values that fail. 如果失败,则开始记录失败的字符串值。 We need to see examples of what didn't work. 我们需要查看不起作用的示例。

Hmm you seem to be doing a lot of unnecessary steps to parse and set a DateTime 嗯,您似乎在执行许多不必要的步骤来解析和设置DateTime

var textDate = "06-20-2018";

if (!string.IsNullOrEmpty(textDate))
{
    var parsedDate = DateTime.ParseExact(textDate, "MM-dd-yyyy", null);

    obj.RefrenceDate = parsedDate;
}

暂无
暂无

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

相关问题 System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime 无法将system.formatexception字符串识别为有效的日期时间 - system.formatexception string was not recognized as a valid datetime System.FormatException:字符串未被识别为有效的布尔值 - System.FormatException: String was not recognized as a valid Boolean c# 中的 DateTime 解析:获取“System.FormatException:”String 未被识别为有效的 DateTime”错误 - DateTime parsing in c#:getting a 'System.FormatException: 'String was not recognized as a valid DateTime' error System.FormatException:字符串未被识别为有效的DateTime(DateTime格式yyyyMMdd:HHmmss) - System.FormatException: String was not recognized as a valid DateTime (DateTime Format yyyyMMdd:HHmmss) 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# 如何解决此错误:System.FormatException:'字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词。 - how to solve this error: System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.' System.FormatException: 'String '"2022-04-14 13:03:12"' 未被识别为有效的 DateTime。' - System.FormatException: 'String '"2022-04-14 13:03:12"' was not recognized as a valid DateTime.' “System.FormatException:‘该字符串未被识别为有效的 DateTime。有一个从索引 0 开始的未知单词。” 在 C# - "System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0." in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM