【问题标题】:Can someone tell me what Date Time format this string is谁能告诉我这个字符串是什么日期时间格式
【发布时间】:2022-11-29 06:51:51
【问题描述】:

我正在通过这个 2022-11-01T00:00:00,日期时间作为字符串。我需要将它转换为 C# 中的 DateTime,但似乎无法弄清楚字符串的确切格式。

我尝试了以下的许多变体,但继续得到 String was not recognized as a valid DateTime.

这是我尝试过的最新代码:

DateTime test = DateTime.ParseExact(startTime, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);

【问题讨论】:

  • 你试过var test = DateTime.Parse(dateString);了吗?

标签: c# datetime-format


【解决方案1】:

是否有您要考虑的抵消?我注意到在您尝试过的最新代码中,日期时间格式的末尾有一个 Z。在此代码块中将其删除使其可解析。

string startTime = "2022-11-01T00:00:00";
DateTime test = DateTime.ParseExact(startTime, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(test.ToString()); // 11/1/2022 12:00:00 AM

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多