简体   繁体   English

“字符串未被识别为有效的日期时间。在读取 CSV 文件时,有一个从索引 0 开始的未知单词”

[英]"String was not recognized as valid Datetime. There is an unknown word starting at index 0" while reading CSV file back

so I'm using CSVhelper to read and write CSV files.所以我使用 CSVhelper 来读写 CSV 个文件。 I have made a class with the following properties to be written into the file.我制作了一个 class 并写入了以下属性。

public class Proddeets {
public string ProductArea { get; set; }
public string Product { get; set; }
public DateTime TimeTaken{ get; set; }
}

When I'm trying to read back the file I get the "String was not recognized as valid Datetime. There is an unknown word starting at index 0" error当我尝试回读文件时,出现“字符串未被识别为有效日期时间。有一个从索引 0 开始的未知单词”错误

I've tried fixing it by creating a function for the TimeTaken prop我尝试通过为 TimeTaken 道具创建一个 function 来修复它

public void SetTimeTaken()
{DateTime today= DateTime.Today;
String s = today.ToString("dd-MM-yyyy");
TimeTaken=s.Trim();
}

I get the 'cannot implicitly convert DateTime to string' error.我收到“无法将 DateTime 隐式转换为字符串”错误。 I'm new to C# and I'm not sure how to go about this, please help我是 C# 的新手,我不确定如何与 go 联系,请帮忙

Your are trying to set the value of a datetime with function that returns a string, try using TimeTaken=Convert.ToDateTime(s);您正在尝试使用返回字符串的 function 设置日期时间的值,请尝试使用TimeTaken=Convert.ToDateTime(s); instead反而

And use string instead of String as a good practice.并使用string而不是String作为一种好的做法。

暂无
暂无

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

相关问题 FormatException:该字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词 - FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0 该字符串未被识别为有效的DateTime。 从索引26开始有一个未知词 - The string was not recognized as a valid DateTime. There is an unknown word starting at index 26 字符串“未定义”未被识别为有效的日期时间。 有一个从索引“0”开始的未知单词 - The string 'undefined' was not recognized as a valid DateTime. There is an unknown word starting at index '0' 该字符串未被识别为有效的 DateTime。 从索引 0 开始有一个未知单词 - The string was not recognized as a valid DateTime. There is an unknown word starting at index 0 无法识别为有效的日期时间。 从索引0开始的未知单词 - Not Recognized as a valid datetime. Unknown word starting at index 0 该字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词。 - The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. label to string 如何解决此错误: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:‘该字符串未被识别为有效的 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# 发生异常:-“未将字符串识别为有效的DateTime。 从索引0开始有一个未知词。” - The Exception occured :- “The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.” 该字符串未被识别为有效的DateTime,从索引25开始有一个未知单词 - The string was not recognized as a valid DateTime, There is an unknown word starting at index 25
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM