【问题标题】:DateTime.TryParseExact Unable to Match FormatDateTime.TryParseExact 无法匹配格式
【发布时间】:2020-09-20 09:19:26
【问题描述】:

我尝试TryParseExact 的日期时间字符串,但它总是无法识别,而是返回false

我可以知道我可能出错的地方吗?

public static string ConvertToLocalDateString(string dateTimeString)
{
    dateTimeString = "06/04/2020 16:00:00";

    if (!string.IsNullOrEmpty(dateTimeString))
    {
        DateTime localDateTime;
        if (DateTime.TryParseExact(dateTimeString, "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out localDateTime))
        {
            return localDateTime.AddHours(8).ToString("d/MMM/yyyy");
        }
    }

    return ""; //returns here
}

【问题讨论】:

  • “返回 null”是什么意思。返回值或者TryParseExact是bool,所以不能是null
  • 对造成的混乱表示歉意。我更新了我的帖子,它返回""

标签: c# datetime tryparse


【解决方案1】:

24h 格式的小时格式是“HH”,而不是“hh”,因此将“dd/MM/yyyy hh:mm:ss”替换为“dd/MM/yyyy HH:mm:ss”

【讨论】:

    猜你喜欢
    • 2015-01-04
    • 2012-08-13
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多