Esempio n. 1
0
        internal static string AutoCompleteDate(string origtext)
        {
            string trimtext = origtext.Trim();

            if (trimtext == "")
            {
                return("");
            }

            // retrieve Locale information
            var dateorder = GlobalizationHelper.DateOrder();

            // split the textbox.value up in 1,2 or 3 parts
            string[] values = SplitInto3(trimtext);

            // It is possible that the parts are all non-numeric
            if (values[0].Length + values[1].Length + values[2].Length == 0)
            {
                return(origtext);
            }

            #region Support for unseparated dates xxyy, xxyyzz or xxyyzzzz
            if (values[0].Length > 0 && values[1].Length == 0 && values[2].Length == 0)
            {
                string temp = values[0];

                switch (temp.Length)
                {
                case 4:
                    values[0] = temp.Substring(0, 2);
                    values[1] = temp.Substring(2, 2);
                    values[2] = "";
                    break;

                case 6:
                    values[0] = temp.Substring(0, 2);
                    values[1] = temp.Substring(2, 2);
                    values[2] = temp.Substring(4, 2);
                    break;

                case 8:

                    if (dateorder == DateOrder.YMD)
                    {
                        values[0] = temp.Substring(0, 4);
                        values[1] = temp.Substring(4, 2);
                        values[2] = temp.Substring(6, 2);
                    }
                    else     // DMY or MDY
                    {
                        values[0] = temp.Substring(0, 2);
                        values[1] = temp.Substring(2, 2);
                        values[2] = temp.Substring(4, 4);
                    }
                    break;
                }
            }
            #endregion

            // test a new datetime value
            DateTime?candidate_value = null;

            try // If the Date is invalid, there will be an Exception
            {
                #region Convert the 0, 1, 2 or 3-string-parts to a DateTime value, throws Exception upon failure

                if (values[0].Length > 0 && values[1].Length == 0 && values[2].Length == 0)
                {
                    candidate_value = new DateTime(DateTime.Now.Year,
                                                   DateTime.Now.Month,
                                                   Convert.ToInt32(values[0]));
                }
                else if (values[0].Length > 0 && values[1].Length > 0 && values[2].Length == 0 && dateorder == DateOrder.MDY)
                {
                    candidate_value = new DateTime(DateTime.Now.Year,
                                                   Convert.ToInt32(values[0]),
                                                   Convert.ToInt32(values[1]));
                }
                else if (values[0].Length > 0 && values[1].Length > 0 &&
                         values[2].Length == 0 && (dateorder == DateOrder.DMY | dateorder == DateOrder.YMD))
                {
                    candidate_value = new DateTime(DateTime.Now.Year,
                                                   Convert.ToInt32(values[1]),
                                                   Convert.ToInt32(values[0]));
                }
                else if (dateorder == DateOrder.DMY)
                {
                    int year = Convert.ToInt32(values[2]);

                    if (values[2].Length <= 2 && year < 100)
                    {
                        year = GlobalizationHelper.ToFourDigitYear(year);
                    }

                    candidate_value = new DateTime(year,
                                                   Convert.ToInt32(values[1]),
                                                   Convert.ToInt32(values[0]));
                }
                else if (dateorder == DateOrder.MDY)
                {
                    int year = Convert.ToInt32(values[2]);

                    if (values[2].Length <= 2 && year < 100)
                    {
                        year = GlobalizationHelper.ToFourDigitYear(year);
                    }

                    candidate_value = new DateTime(year,
                                                   Convert.ToInt32(values[0]),
                                                   Convert.ToInt32(values[1]));
                }
                else if (dateorder == DateOrder.YMD)
                {
                    int year = Convert.ToInt32(values[0]);

                    if (values[0].Length <= 2 && year < 100)
                    {
                        year = GlobalizationHelper.ToFourDigitYear(year);
                    }

                    candidate_value = new DateTime(year,
                                                   Convert.ToInt32(values[1]),
                                                   Convert.ToInt32(values[2]));
                }

                #endregion

                if (candidate_value == null)
                {
                    return(trimtext);
                }

                return(candidate_value.Value.ToShortDateString());
            }
            catch (Exception)
            {
                return(trimtext);
            }
        } // end of method