Check several date formats using DateTime.TryParse()

后端 未结 5 1986
北荒
北荒 2021-01-05 02:34

I\'m using a method to validate textboxes.

    public bool ValidateDateTimeTextBoxes(params TextBox[] textBoxes)
    {
        DateTime value = DateTime.Toda         


        
相关标签:
5条回答
  • 2021-01-05 02:47

    Try using TryParseExact

    Converts the specified string representation of a date and time to its DateTime equivalent. 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);
    
    0 讨论(0)
  • 2021-01-05 02:52
     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;
        }
    
    0 讨论(0)
  • 2021-01-05 03:04
    DateTime.TryParseExact(textBox.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out outDt))
    
    0 讨论(0)
  • 2021-01-05 03:04

    Use TryParseExact instead which is also faster. 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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-05 03:06

    Try 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.

    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. Here mm represents the minute not the month. Use MM for the month representation.

    0 讨论(0)
提交回复
热议问题