コード例 #1
0
        public static bool TryParseCustomDates(FormCollection form, out string errorMessage)
        {
            string[]      formKeys = form.AllKeys;
            List <string> days     = formKeys.Where(x => x.EndsWith("_Day")).ToList();

            foreach (string dayKey in days)
            {
                string fieldId      = dayKey.Replace("_Day", "");
                int    day          = Convert.ToInt32(form[fieldId + "_Day"]);
                int    month        = Convert.ToInt32(form[fieldId + "_Month"]);
                int    year         = Convert.ToInt32(form[fieldId + "_Year"]);
                bool   containsTime = formKeys.Contains(fieldId + "_Hour");
                int    hour         = containsTime && !string.IsNullOrEmpty(form[fieldId + "_Hour"]) ? Convert.ToInt32(form[fieldId + "_Hour"]) : -1;
                int    minute       = containsTime && !string.IsNullOrEmpty(form[fieldId + "_Minute"]) ? Convert.ToInt32(form[fieldId + "_Minute"]) : -1;
                if (day == 0 && month == 0 && year == 0 && hour == -1 && minute == -1)
                {
                    form[fieldId] = null;
                    continue;
                }
                string[] keyparts  = fieldId.Split('_');
                string   fieldName = keyparts[2];
                if (hour == -1 && minute >= 0 || hour >= 0 && minute == -1)
                {
                    errorMessage = string.Format(Translator.GetMessageString("errInvalidDateTimeFormat"), Translator.GetString(fieldName));
                    return(false);
                }
                if (day == 0 || month == 0 || year == 0)
                {
                    errorMessage = string.Format(containsTime ? Translator.GetMessageString("errInvalidDateTimeFormat") :
                                                 Translator.GetMessageString("errInvalidDateFormat"), Translator.GetString(fieldName));
                    return(false);
                }
                DateTime date;
                try
                {
                    hour   = hour == -1 ? 0 : hour;
                    minute = minute == -1 ? 0 : minute;
                    date   = containsTime ? new DateTime(year - Localizer.YearShift, month, day, hour, minute, 0) : new DateTime(year - Localizer.YearShift, month, day);
                }
                catch (Exception)
                {
                    errorMessage = string.Format(Translator.GetMessageString("errInvalidDateFormat"), Translator.GetString(fieldName));
                    return(false);
                }
                //form[fieldId] = date.ToString("MM/dd/yyyy HH:mm:ss");
                form[fieldId] = date.ToShortDateString();
            }
            errorMessage = string.Empty;
            return(true);
        }
コード例 #2
0
 static void object_ValidationDetails(object sender, ValidationEventArgs args)
 {
     m_error = Translator.GetErrorMessage(args);
 }