Posts
247
Comments
67
Trackbacks
4
Kleine Funktion für ein gültiges Datum

Hier eine kleine Funktion welche überprüft, ob ein übergebenes Objekt ein Datum ist und diesen Wert als Datum zurückgibt: 

            static public DateTime ToValidDateTime(object n) {

                  if(n==null) return DateTime.MinValue;
                  if(n.GetType() == typeof(DateTime)) {
                        if((DateTime)n < DateTime.MinValue) {
                             return DateTime.MinValue;
                        } else {
                             return (DateTime)n;
                        }
                  }

                  try {
                        if(n.GetType() == typeof(string)) {
                             return DateTime.Parse( (string)n, sFormat );
                        }
                  }
                  catch( FormatException ) {
                  }

                  return DateTime.MinValue;
            }

posted on Friday, February 01, 2008 9:06 AM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 1 and 3 and type the answer here: