简体   繁体   English

使用DateTime.TryParse()检查多种日期格式

[英]Check several date formats using DateTime.TryParse()

I'm using a method to validate textboxes. 我正在使用一种方法来验证文本框。

    public bool ValidateDateTimeTextBoxes(params TextBox[] textBoxes)
    {
        DateTime value = DateTime.Today;
        //string dateFormat = "dd/mm/yyyy";

        foreach (var textBox in textBoxes)
        {
            if (!DateTime.TryParse(textBox.Text, out value))
            {
                return false;
            }
        }

        return true;
    }

I want to check the format too. 我也想查看格式。 It requires mm/dd/yyyy , but want it to be dd/mm/yyyy 它需要mm/dd/yyyy ,但希望它是dd/mm/yyyy

Try DateTime.TryParseExact 尝试DateTime.TryParseExact

DateTime dt;

DateTime.TryParseExact(textBox.Text, 
                       "dd/MM/yyyy", 
                       CultureInfo.InvariantCulture, 
                       DateTimeStyles.None, 
                       out dt);

If you want to check multiple formats as you updated in your question then you can do using another overload method of TryParseExact which takes format parameter as array of string. 如果要在问题中更新时检查多种格式,则可以使用另一种TryParseExact重载方法,该方法将format参数作为字符串数组。

string[] formats = { "dd/MM/yyyy", "MM/dd/yyyy" };
DateTime.TryParseExact(txtBox.Text, 
                       formats, 
                       CultureInfo.InvariantCulture, 
                       DateTimeStyles.None, 
                       out value));

Please take care of format string. 请注意格式字符串。 As you have mentioned format as dd/mm/yyyy . 正如你所提到的格式为dd/mm/yyyy Here mm represents the minute not the month. 这里mm表示minute而不是月份。 Use MM for the month representation. 使用MM表示月份。

DateTime.TryParseExact(textBox.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out outDt))
 public bool ValidateDateTimeTextBoxes(params TextBox[] textBoxes)
    {
        DateTime value = DateTime.Now;
        //string dateFormat = "dd/mm/yyyy";

        foreach (var textBox in textBoxes)
        {
            if (!DateTime.TryParse(textBox.Text,"dd/mm/yyyy",new CultureInfo("en-US"), 
                          DateTimeStyles.None out value))
            {
                return false;
            }
        }

        return true;
    }

Try using TryParseExact 尝试使用TryParseExact

Converts the specified string representation of a date and time to its DateTime equivalent. 将指定的日期和时间字符串表示形式转换为其DateTime等效项。 The format of the string representation must match a specified format exactly. 字符串表示的格式必须与指定的格式完全匹配。 The method returns a value that indicates whether the conversion succeeded. 该方法返回一个值,指示转换是否成功。

DateTime.TryParseExact(DateValue, 
                       "dd/mm/yyyy", 
                       CultureInfo.InvariantCulture, 
                       DateTimeStyles.None, 
                       out outDatetime);

Use TryParseExact instead which is also faster. 使用TryParseExact而不是更快。 Example: 例:

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        string dateString = "27/05/2012"; // <-- Valid
        string dtformat = "dd/mm/yyyy";
        DateTime dateTime;
        if (DateTime.TryParseExact(dateString, dtformat, CultureInfo.InvariantCulture,
            DateTimeStyles.None, out dateTime))
        {
           Console.WriteLine(dateTime);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM