2013-06-20 75 views
1

我有以下方法,它能正常工作,只要处理一个真正的无效/有效日期,但是,如果我遇到一个空的字符串或日期与掩码如__/__/____,我想通过那些有效,但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; 
} 
+0

你几乎肯定会写一个正则表达式来处理这个问题。 – Yuck

+0

看看这里:http://stackoverflow.com/questions/4962276/best-way-to-get-a-date-with-net – frenchie

+0

@Yuck - 我想到了正则表达式。你能够提供答案吗? – Xaisoft

回答

4

可你只是看看在运行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; 
} 
+0

我想你可能是对的。我已经尝试了一切,所以我会给这个镜头。 – Xaisoft

+0

如果 –

+0

@MattJohnson错过了第二个'!' - 效果很好,我只做了一个小小的修改,因为我处理了其他验证,我不能只返回成功的日期,因此请查看更新的方法并让我知道它看起来不错吗? – Xaisoft

1

有很多方法去皮肤这只猫。我想这取决于你想要视为有效的多少无效数据。此外,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; 
} 

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

+0

你能详细阐述一下文化吗?无论文化如何,我的日期都将与“en-US”文化一起存储? – Xaisoft

+2

作为'DateTime'存储,文化无关紧要。但是,作为字符串,他们确实。你怎么知道用户会输入mm/dd/yyyy或dd/mm/yyyy?你确定分隔符总是一个正斜杠,或者可能是一个短划线或一段时间?如果一年到来会怎样?如果你将要了解文化,那么你必须考虑这些事情。如果您要锁定某种特定的文化,那么您应该使用'TryParseExact'并将格式和文化传入。 –