Xceed.Wpf.Toolkit.DateTimeParser.TryParse C# (CSharp) Méthode

TryParse() public static méthode

public static TryParse ( string value, string format, System.DateTime currentDate, CultureInfo cultureInfo, System.DateTime &result ) : bool
value string
format string
currentDate System.DateTime
cultureInfo System.Globalization.CultureInfo
result System.DateTime
Résultat bool
    public static bool TryParse( string value, string format, DateTime currentDate, CultureInfo cultureInfo, out DateTime result )
    {
      bool success = false;
      result = currentDate;

      if( string.IsNullOrEmpty( value ) || string.IsNullOrEmpty( format ) )
        return false;

      var dateTimeString = ComputeDateTimeString( value, format, currentDate, cultureInfo ).Trim();

      if( !String.IsNullOrEmpty( dateTimeString ) )
        success = DateTime.TryParse( dateTimeString, cultureInfo.DateTimeFormat, DateTimeStyles.None, out result );

      if( !success )
        result = currentDate;

      return success;
    }

Usage Example

        private bool TryParseDateTime(string text, out DateTime result)
        {
            bool isValid = false;

            result = this.ContextNow;

            DateTime current = this.ContextNow;

            try
            {
                current = (this.Value.HasValue)
                    ? this.Value.Value
                    : DateTime.Parse(this.ContextNow.ToString(), this.CultureInfo.DateTimeFormat);

                isValid = DateTimeParser.TryParse(text, this.GetFormatString(Format), current, this.CultureInfo, out result);
            }
            catch (FormatException)
            {
                isValid = false;
            }

            if (!isValid)
            {
                isValid = DateTime.TryParseExact(text, this.GetFormatString(this.Format), this.CultureInfo, DateTimeStyles.None, out result);
            }

            if (!isValid)
            {
                result = (_lastValidDate != null) ? _lastValidDate.Value : current;
            }

            return(isValid);
        }
All Usage Examples Of Xceed.Wpf.Toolkit.DateTimeParser::TryParse