字符串“22-05-1998”未被识别为有效的日期时间

发布于 2025-01-16 13:16:34 字数 1453 浏览 0 评论 0原文

我正在使用 CsvHelper 库从 CSV 文件读取数据,如下所示

this< /a>

我的代码:

public class CsvTransaction
{

    [Name("Account")]
    public string Account { get; set; }
    [Name("Amount")]
    public int Amount { get; set; }
    [Name("Date")]
    public DateTime Date { get; set; }
    [Name("Note")]
    public string Note { get; set; }
}

public void call()
            {
                using (var StreamReader = new StreamReader(@"C:\Users\santo\Downloads\industry.csv"))
                {
                    using (var csvReader = new CsvReader(StreamReader, CultureInfo.InvariantCulture))
                    {
                        var records = csvReader.GetRecords<CsvTransaction>().ToList();
                        Console.WriteLine(records.Count);
                        foreach(var item in records)
                         {
                            Console.WriteLine(item.Amount.GetType());
                            Console.WriteLine(item.Note.GetType());
                        

                        CsvAddTransaction(item.Account, item.Amount, item.Date, item.Note);
                    }
                    }
                }
            }

当我调用这个 call() 时,它说 String '22-05-1998' 未被识别为有效的日期时间。所有其他都给出了我需要的确切数据类型,但是 item.Date 有问题

有人能帮我解决这个问题吗?

I am using CsvHelper library to read the data from a CSV file looks like

this

My code:

public class CsvTransaction
{

    [Name("Account")]
    public string Account { get; set; }
    [Name("Amount")]
    public int Amount { get; set; }
    [Name("Date")]
    public DateTime Date { get; set; }
    [Name("Note")]
    public string Note { get; set; }
}

public void call()
            {
                using (var StreamReader = new StreamReader(@"C:\Users\santo\Downloads\industry.csv"))
                {
                    using (var csvReader = new CsvReader(StreamReader, CultureInfo.InvariantCulture))
                    {
                        var records = csvReader.GetRecords<CsvTransaction>().ToList();
                        Console.WriteLine(records.Count);
                        foreach(var item in records)
                         {
                            Console.WriteLine(item.Amount.GetType());
                            Console.WriteLine(item.Note.GetType());
                        

                        CsvAddTransaction(item.Account, item.Amount, item.Date, item.Note);
                    }
                    }
                }
            }

when I call this call(), it is saying String '22-05-1998' was not recognized as a valid DateTime. all the other are giving exact datatypes I needed, but there is a problem with item.Date

Can anyone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2025-01-23 13:16:34

您必须定义日期时间格式,您会收到此错误,因为解析机制可能需要类似“MM-dd-yyyy”的内容,而不是它,而是收到“dd-MM-yyyy”。

这是文档 详细描述了 DateTime 格式。
此处是您问题的潜在解决方案的网址。

You have to define DateTime format, you are getting this error because parsing mechanism might be expecting something like "MM-dd-yyyy" and instead of that, it receives "dd-MM-yyyy".

Here's documentation which describes DateTime formatting in detail.
And here is url to potential solution to your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文