c# 日期时间未被识别为有效的日期时间

问题描述 投票:0回答:1

我正在尝试读取 Icalendar 文件,但这是我收到的错误: 我正在使用 Godot 4.2,但我认为这应该没有什么区别。

E 0:00:00:0432   System.DateTime System.DateTime.ParseExact(string, string, System.IFormatProvider): System.FormatException: String '20231120T153000' was not recognized as a valid DateTime.
  <C++ Error>    System.FormatException
  <C++ Source>   :0 @ System.DateTime System.DateTime.ParseExact(string, string, System.IFormatProvider)
  <Stack Trace>  :0 @ System.DateTime System.DateTime.ParseExact(string, string, System.IFormatProvider)
                 Application.cs:77 @ System.DateTime Application.ParseICSDateTime(string)
                 Application.cs:49 @ void Application.ReadFile(string)
                 Application.cs:23 @ void Application._Ready()
                 Node.cs:2117 @ bool Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
                 CanvasItem.cs:1368 @ bool Godot.CanvasItem.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
                 Control.cs:2841 @ bool Godot.Control.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
                 Application_ScriptMethods.generated.cs:58 @ bool Application.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
                 CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(nint, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant_call_error*, Godot.NativeInterop.godot_variant*)

我解析的方式:

在评论中我放置了“无效”日期时间,下划线是为了便于阅读。


private const string DATE_TIME_FORMAT = "yyyyMMddThhmmss";

    private static DateTime ParseICSDateTime(string line)
    {
        // 2023_11_20_T_15_30_00
        // yyyy_MM_dd_T_hh_mm_ss
        
        string dateTimeString = line.Split(":")[1];
        GD.Print(dateTimeString);
        
        DateTime result = DateTime.ParseExact(dateTimeString, DATE_TIME_FORMAT, CultureInfo.InvariantCulture);
        return result;
    }

我不确定为什么这个日期时间特别无效,因为对于其他时间来说它工作正常。 查看控制台的图像 Console screenshot

我尝试了 null

IFormatProvider
,但这也不起作用。

我还尝试删除 dateTimeString 内部的 T,但同样没有骰子。

c# datetime datetime-format icalendar
1个回答
0
投票

小时格式应为“HH”,表示 24 小时时间,而不是 hh(AM 和 PM 重复 12 小时)。

private const string DATE_TIME_FORMAT = "yyyyMMddTHHmmss";
© www.soinside.com 2019 - 2024. All rights reserved.