【问题标题】:Handling Invalid Dates as Valid?将无效日期视为有效日期?
【发布时间】:2013-06-20 01:06:25
【问题描述】:

我有以下方法,它可以正确处理真正的无效/有效日期,但是,如果我遇到空字符串或带有 __/__/____ 等掩码的日期,我想将它们作为有效日期传递,但DateTime.TryParse 使它们无效。如何修改以下方法以传递我的无效场景?下面的方法是一个示例程序:

public bool ValidateDate(string date, out string message)
{
    bool success = true;
    message = string.Empty;
    DateTime dateTime;

    if(DateTime.TryParse(date,out dateTime))
    {
        success = false;
        message = "Date is Invalid";
    }

    return success;
}

void Main()
{
    //The only date below that should return false is date4.

    string date = "01/01/2020";
    string date2 = "";
    string date3 = "__/__/____";
    string date4 = "44/__/2013";

    string message;

    ValidateDate(date, out message); //Should return true
    ValidateDate(date2, out message); //Should return true
    ValidateDate(date3, out message); //Should return true
    ValidateDate(date4, out message); //Should return false
}

我无法将其更改为 if(!DateTime.TryParse(date3,out dateTime)),因为这将对我想要验证的日期返回 false。

我也尝试过类似if(!date3.contains("_") && DateTime.TryParse(date3,out dateTime)) 的操作,但这仍然失败。我应该颠倒我的验证顺序吗?问题是我不只是在第一个无效日期返回 false,我正在构建所有无效日期的 StringBuilder 然后返回它,所以我没想到:

if(DateTime.TryParse(date3,out dateTime))
        return true;
    else
        return true;

public bool ValidateDate(string date, out string message)
{
    string[] overrides = {"","__/__/____"};

    bool success = true;
    message = string.Empty;
    DateTime dateTime;

    if(!overrides.Contains(date) && !DateTime.TryParse(date,out dateTime))
    {
        success = false;
        message = "Date is Invalid";
    }


    return success;
}

【问题讨论】:

  • 您几乎可以肯定必须编写一个正则表达式来处理这个问题。
  • @Yuck - 我想到了正则表达式。你能提供一个答案吗?

标签: c# parsing datetime


【解决方案1】:

在运行DateTime.TryParse 方法之前,您能否只查看一组覆盖?

static string[] overrides = { "", "__/__/____" };
public bool ValidateDate(string date, out string message)
{
    bool success = true;
    message = string.Empty;

    if(overrides.Contains(date)) { return success; }

    DateTime dateTime;

    if(!DateTime.TryParse(date,out dateTime))
    {
        success = false;
        message = "Date is Invalid";
    }


    return success;
}

【讨论】:

  • 我认为你可能是对的。我已经尝试了一切,所以我会试一试。
  • 在第二个 if 中缺少 !
  • @MattJohnson - 效果很好,我只是做了一点修改,因为我处理了其他验证,我不能只返回一个成功的日期,所以看看更新的方法,让我知道是否看起来不错?
  • 所以...如果成功解析日期,您想失败吗?
  • 是的,我只是从原始帖子中复制并粘贴并添加了两行
【解决方案2】:

有很多方法可以给这只猫剥皮。我想这取决于您想将多少无效数据视为有效数据。另外,DateTime.TryParse 会考虑当前的文化设置,所以也许你也应该考虑?

bool ValidateDate(string date, out string message)
{
    message = string.Empty;

    if (date == null)
        return true;

    const string mask = "_";
    var separator = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator;
    var test = date.Replace(mask, string.Empty).Replace(separator, string.Empty);
    if (string.IsNullOrWhiteSpace(test))
        return true;

    DateTime dateTime;
    if (!DateTime.TryParse(date, out dateTime))
    {
        message = "Date is Invalid";
        return false;
    }

    return true;
}

我想你也可以用正则表达式来做到这一点。有很多有效的解决方案。

【讨论】:

  • 您能详细说明一下文化吗?无论文化如何,我的日期都会以en-US 文化存储?
  • 存储为DateTime,文化无关紧要。但作为字符串,它们确实如此。你怎么知道用户将输入 mm/dd/yyyy 或 dd/mm/yyyy?您确定分隔符将始终是正斜杠,还是破折号或句点?如果一年是第一位的呢?如果您要具有文化意识,那么您必须考虑这些事情。如果您要锁定特定的文化,那么您可能应该使用TryParseExact 并传递格式和文化。
猜你喜欢
  • 2014-06-24
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多