public static string Format(BirthDate bdate, BirthDateFormatInfo info)
 {
     if (bdate == null) throw new ArgumentNullException("bdate");
     if (info == null) info = new BirthDateFormatInfo();
     
     return Format(bdate, info.Pattern, info.RepresentMissingComponentsWithX);
 }
        public static string GetFieldWithoutControl(this BirthDate bdate)
        {
            const int max = 6;

            var y = bdate.Year == 0 ?
                    DoubleFillerString : bdate.Year.ToString("0000").Substring(2, 2);
            var m = bdate.Month.ToString("00").Replace("00", DoubleFillerString);
            var d = bdate.Day.ToString("00").Replace("00", DoubleFillerString);

            var text = y + m + d;

            return(GetFieldWithoutControl(text, max));
        }
Beispiel #3
0
            public static string Format(BirthDate bdate, BirthDateFormatInfo info)
            {
                if (bdate == null)
                {
                    throw new ArgumentNullException("bdate");
                }
                if (info == null)
                {
                    info = new BirthDateFormatInfo();
                }

                return(Format(bdate, info.Pattern, info.RepresentMissingComponentsWithX));
            }
Beispiel #4
0
 /// <summary>
 /// Converts the string representation of a birth date to a <see cref="BirthDate" /> object.
 /// The return value indicates whether the conversion succeeded.
 /// </summary>
 /// <param name="text">The text to parse.</param>
 /// <param name="result">When this method returns, contains <see cref="BirthDate" /> object equivalent
 /// to the birth date contained in <paramref name="text" /> if the conversion succeeded;
 /// or <see cref="BirthDate.Empty" /> if the conversion failed.
 /// The conversion fails if <paramref name="text" /> is null or empty or is not
 /// in the correct format. This parameter is passed uninitialized.</param>
 /// <param name="info">An object containing formatting and parsing information.</param>
 /// <returns>
 ///   <c>True</c> if <paramref name="text" /> was converted successfully; otherwise, false.
 /// </returns>
 public static bool TryParse(string text, out BirthDate result, BirthDateFormatInfo info)
 {
     result = BirthDate.Empty;
     try
     {
         result = Parse(text, info);
         return(true);
     }
     catch (Exception ex)
     {
         var debugException = ex;
         return(false);
     }
 }
Beispiel #5
0
 /// <summary>
 /// Converts the string representation of a birth date to a <see cref="BirthDate" /> object.
 /// The return value indicates whether the conversion succeeded.
 /// </summary>
 /// <param name="text">The text to parse.</param>
 /// <param name="result">When this method returns, contains <see cref="BirthDate" /> object equivalent
 /// to the birth date contained in <paramref name="text" /> if the conversion succeeded;
 /// or <see cref="BirthDate.Empty" /> if the conversion failed.
 /// The conversion fails if <paramref name="text" /> is null or empty or is not
 /// in the correct format. This parameter is passed uninitialized.</param>
 /// <param name="culture">The culture used to parse.</param>
 /// <returns>
 ///   <c>True</c> if <paramref name="text" /> was converted successfully; otherwise, false.
 /// </returns>
 public static bool TryParse(string text, out BirthDate result, CultureInfo culture = null)
 {
     result = BirthDate.Empty;
     try
     {
         result = Parse(text, culture);
         return(true);
     }
     catch (Exception ex)
     {
         var debugException = ex;
         return(false);
     }
 }
Beispiel #6
0
            private static string Format(BirthDate bdate, string pattern, bool useX)
            {
                int step    = 0;
                var builder = new StringBuilder();

                for (int i = 0; i < pattern.Length; i += step)
                {
                    char patternChar = pattern[i];
                    switch (patternChar)
                    {
                    case 'y':
                        var year = bdate.Year;
                        step = ParseRepeatPattern(pattern, i, patternChar);
                        FormatDigits(builder, year, step <= 2 ? 2 : 4, useX, forceSubstring: true);     // either 4 or 2!
                        break;

                    case 'M':
                        var month = bdate.Month;
                        step = ParseRepeatPattern(pattern, i, patternChar);
                        if (step > 2)     // We do not support (yet) string-based month representations
                        {
                            step = 2;
                        }
                        FormatDigits(builder, month, step, useX);
                        break;

                    case 'd':
                        var day = bdate.Day;
                        step = ParseRepeatPattern(pattern, i, patternChar);
                        if (step > 2)     // We do not support (yet) string-based day representations
                        {
                            step = 2;
                        }
                        FormatDigits(builder, day, step, useX);
                        break;

                    default: // Other characters are separators; they are simply copied to the output buffer
                        builder.Append(patternChar);
                        step = 1;
                        break;
                    }
                }

                return(builder.ToString());
            }
            private static string Format(BirthDate bdate, string pattern, bool useX)
            {
                int step = 0;
                var builder = new StringBuilder();
                for (int i = 0; i < pattern.Length; i += step)
                {
                    char patternChar = pattern[i];
                    switch (patternChar)
                    {
                        case 'y':
                            var year = bdate.Year;
                            step = ParseRepeatPattern(pattern, i, patternChar);
                            FormatDigits(builder, year, step <= 2 ? 2 : 4, useX, forceSubstring: true); // either 4 or 2!
                            break;
                        case 'M':
                            var month = bdate.Month;
                            step = ParseRepeatPattern(pattern, i, patternChar);
                            if (step > 2) // We do not support (yet) string-based month representations
                                step = 2;
                            FormatDigits(builder, month, step, useX);
                            break;
                        case 'd':
                            var day = bdate.Day;
                            step = ParseRepeatPattern(pattern, i, patternChar);
                            if (step > 2) // We do not support (yet) string-based day representations
                                step = 2;
                            FormatDigits(builder, day, step, useX);
                            break;
                    default: // Other characters are separators; they are simply copied to the output buffer
                            builder.Append(patternChar);
                            step = 1;
                            break;
                    }
                }

                return builder.ToString();
            }
        public static string GetFieldWithControl(this BirthDate bdate)
        {
            var without = GetFieldWithoutControl(bdate);

            return(GetFieldWithControl(without, 6));
        }
 /// <summary>
 /// Converts the string representation of a birth date to a <see cref="BirthDate" /> object.
 /// The return value indicates whether the conversion succeeded.
 /// </summary>
 /// <param name="text">The text to parse.</param>
 /// <param name="result">When this method returns, contains <see cref="BirthDate" /> object equivalent
 /// to the birth date contained in <paramref name="text" /> if the conversion succeeded;
 /// or <see cref="BirthDate.Empty" /> if the conversion failed.
 /// The conversion fails if <paramref name="text" /> is null or empty or is not
 /// in the correct format. This parameter is passed uninitialized.</param>
 /// <param name="info">An object containing formatting and parsing information.</param>
 /// <returns>
 ///   <c>True</c> if <paramref name="text" /> was converted successfully; otherwise, false.
 /// </returns>
 public static bool TryParse(string text, out BirthDate result, BirthDateFormatInfo info)
 {
     result = BirthDate.Empty;
     try
     {
         result = Parse(text, info);
         return true;
     }
     catch (Exception ex)
     {
         var debugException = ex;
         return false;
     }
 }
 /// <summary>
 /// Converts the string representation of a birth date to a <see cref="BirthDate" /> object.
 /// The return value indicates whether the conversion succeeded.
 /// </summary>
 /// <param name="text">The text to parse.</param>
 /// <param name="result">When this method returns, contains <see cref="BirthDate" /> object equivalent
 /// to the birth date contained in <paramref name="text" /> if the conversion succeeded;
 /// or <see cref="BirthDate.Empty" /> if the conversion failed.
 /// The conversion fails if <paramref name="text" /> is null or empty or is not
 /// in the correct format. This parameter is passed uninitialized.</param>
 /// <param name="culture">The culture used to parse.</param>
 /// <returns>
 ///   <c>True</c> if <paramref name="text" /> was converted successfully; otherwise, false.
 /// </returns>
 public static bool TryParse(string text, out BirthDate result, CultureInfo culture = null)
 {
     result = BirthDate.Empty;
     try
     {
         result = Parse(text, culture);
         return true;
     }
     catch (Exception ex)
     {
         var debugException = ex;
         return false;
     }
 }