[英]"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.