簡體   English   中英

c# 中的 DateTime 解析:獲取“System.FormatException:”String 未被識別為有效的 DateTime”錯誤

[英]DateTime parsing in c#:getting a 'System.FormatException: 'String was not recognized as a valid DateTime' error

我在本地文件夾的 .csv 文件中有一個數據集,下面是數據的示例行,每個項目有 13 個屬性。

我正在用 C# 解析這些數據,我的代碼已經工作了 2 年,我不記得了

讀取.csv 文件的代碼是,這部分是將數據解析成compiledList。

static string loadFile(string fileLocation)
    {
        string text = "";

        try
        {
            text = File.ReadAllText(fileLocation);
        }
        catch (Exception e)
        {
            Console.WriteLine("An error has occured...");
            Console.WriteLine(e.Message);
        }

        return text;
    }
    static ConcurrentBag<Item> interpretFile(string text, ConcurrentBag<Item> compiledList)
    {
        String[] substrings = text.Split('\n');
        int settlementPeriod = -1;   int totalSP = -1;

        foreach (string line in substrings)
        {
            String[] items = line.Split(',');
            if (items[0] == "HDR")
            {
                settlementPeriod = int.Parse(items[3]);
                if (settlementPeriod > 48)
                    settlementPeriod -= 48;
                if (settlementPeriod < 0)
                    settlementPeriod += 48;

                totalSP = getTotalSettlementPeriod(DateTime.ParseExact(items[2], "yyyyMMdd", null), settlementPeriod);
            }
            if (items[0] == "BOALF")
            {
                //Item Bid = new Item(items);
                Item Bid = new Item
                {
                    recordType = items[0],
                    unitID = items[1],
                    acceptID = float.Parse(items[2]),
                    acceptTime = DateTime.ParseExact(items[3], "yyyyMMddHHmmss", null),
                    deemedFlag = ToBoolean(items[4]),
                    soFlag = ToBoolean(items[5]),
                    storFlag = ToBoolean(items[6]),
                    fromTime = DateTime.ParseExact(items[7], "yyyyMMddHHmmss", null),
                    fromLevel = float.Parse(items[8]),
                    toTime = DateTime.ParseExact(items[9], "yyyyMMddHHmmss", null),
                    toLevel = float.Parse(items[10]),
                    settlementPeriod = settlementPeriod,
                    totalSP = totalSP
                };
                compiledList.Add(Bid);

.csv 中的示例項目是:

在此處輸入圖片說明

當我在 Notebad 中打開數據集時,我看到的是:

在此處輸入圖片說明

請注意, items[0]是 about 樣本數據集中的第一列。 我現在遇到問題的數據是第 4 列,即上面顯示的“2.02E+13”。

那實際上是'20191211202600',它是數字格式的'yyyymmddhhmmss'。 我不知道發生了什么變化,以至於下面給了我一個錯誤。

acceptTime = DateTime.ParseExact(items[3], "yyyyMMddHHmmss", null)

`

我得到的錯誤是:

System.FormatException: 'String 未被識別為有效的 DateTime。'

感謝您的幫助,如果需要進一步說明,請告訴我。

謝謝

我不認為問題出在您的 C# 代碼上,包含日期格式yyyymmddhhmmss的列應該是字符串類型,現在它們被視為數字。 這個問題是由您保存 CSV 文件(例如 excel 或 google 電子表格)的程序引起的,您需要將列數據類型更改為字符串(因為它現在會自動檢測為數字)。

以下在 .NET fiddle ( https://dotnetfiddle.net/ ) 中有效,您能否驗證在DateTime.ParseExact調用中使用的字符串? 也許該字符串不等於您在運行時所期望的。

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(DateTime.ParseExact("20191211202600", "yyyyMMddHHmmss", null));
    }
}

輸出是:

12/11/2019 8:26:00 PM

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM