Exemple #1
0
 public static string GetPmForFormat(DateTimeFormatInfo formatInfo)
 {
     return(formatInfo.IsAmPmShortFormat == true ? ShortPM : LongPM);
 }
Exemple #2
0
        private void ApplyFormat()
        {
            _applyingFormat = true;

            try
            {
                var format = Format;
                _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(format);
                var hasLongTimeFormat = !(_formatInfo.HourFormat is null ||
                                          _formatInfo.MinuteFormat is null ||
                                          _formatInfo.SecondFormat is null);

                var hasAnyTimeFormat = !(_formatInfo.HourFormat is null &&
                                         _formatInfo.MinuteFormat is null &&
                                         _formatInfo.SecondFormat is null);

                SetCurrentValue(HideTimeProperty, !hasAnyTimeFormat || _hideTime);

                if (!hasLongTimeFormat)
                {
                    var timePattern = DateTimeFormatHelper.ExtractTimePatternFromFormat(format);
                    if (!string.IsNullOrEmpty(timePattern))
                    {
                        timePattern = DateTimeFormatHelper.FindMatchedLongTimePattern(CultureInfo.CurrentUICulture, timePattern);
                    }

                    if (string.IsNullOrEmpty(timePattern))
                    {
                        timePattern = CultureInfo.CurrentUICulture.DateTimeFormat.LongTimePattern;
                    }

                    var datePattern = DateTimeFormatHelper.ExtractDatePatternFromFormat(format);

                    format = $"{datePattern} {timePattern}";

                    _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(format);
                }

                if (!hasAnyTimeFormat)
                {
                }

                IsYearShortFormat = _formatInfo.IsYearShortFormat;
                NumericTBYear.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsYearShortFormat ? 0 : 1));
                NumericTBYear.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsYearShortFormat ? 99 : 3000));

                var isHour12Format = _formatInfo.IsHour12Format ?? true;
                IsHour12Format = isHour12Format;
                NumericTBHour.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(isHour12Format ? 1 : 0));
                NumericTBHour.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(isHour12Format ? 12 : 23));
                ToggleButtonH.SetCurrentValue(TagProperty, isHour12Format ? DateTimePart.Hour12 : DateTimePart.Hour);

                IsAmPmShortFormat = _formatInfo.IsAmPmShortFormat.Value;

                EnableOrDisableYearConverterDependingOnFormat();
                EnableOrDisableHourConverterDependingOnFormat();
                EnableOrDisableAmPmConverterDependingOnFormat();

                ListTBAmPm.SetCurrentValue(ListTextBox.ListOfValuesProperty, new List <string>()
                {
                    Meridiems.GetAmForFormat(_formatInfo),
                    Meridiems.GetPmForFormat(_formatInfo)
                });

                NumericTBDay.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.DayFormat.Length));
                NumericTBMonth.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MonthFormat.Length));
                NumericTBYear.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.YearFormat.Length));
                NumericTBHour.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.HourFormat.Length));
                NumericTBMinute.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MinuteFormat.Length));
                NumericTBSecond.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.SecondFormat?.Length ?? 0));

                UnsubscribeNumericTextBoxes();

                Grid.SetColumn(NumericTBDay, GetPosition(_formatInfo.DayPosition));
                Grid.SetColumn(NumericTBMonth, GetPosition(_formatInfo.MonthPosition));
                Grid.SetColumn(NumericTBYear, GetPosition(_formatInfo.YearPosition));
                Grid.SetColumn(NumericTBHour, GetPosition(_formatInfo.HourPosition.Value));
                Grid.SetColumn(NumericTBMinute, GetPosition(_formatInfo.MinutePosition.Value));
                Grid.SetColumn(NumericTBSecond, GetPosition(_formatInfo.SecondPosition ?? _defaultSecondFormatPosition));
                Grid.SetColumn(ListTBAmPm, GetPosition(_formatInfo.AmPmPosition ?? _defaultAmPmFormatPosition));

                Grid.SetColumn(ToggleButtonD, GetPosition(_formatInfo.DayPosition) + 1);
                Grid.SetColumn(ToggleButtonMo, GetPosition(_formatInfo.MonthPosition) + 1);
                Grid.SetColumn(ToggleButtonY, GetPosition(_formatInfo.YearPosition) + 1);
                Grid.SetColumn(ToggleButtonH, GetPosition(_formatInfo.HourPosition.Value) + 1);
                Grid.SetColumn(ToggleButtonM, GetPosition(_formatInfo.MinutePosition.Value) + 1);
                Grid.SetColumn(ToggleButtonS, GetPosition(_formatInfo.SecondPosition ?? _defaultSecondFormatPosition) + 1);

                //hide parts according to Hide options and format
                bool isHiddenManual = HideSeconds || HideTime;

                NumericTBSecond.SetCurrentValue(NumericTextBox.VisibilityProperty, _formatInfo.SecondFormat is null || isHiddenManual ? Visibility.Collapsed : Visibility.Visible);
                ToggleButtonS.SetCurrentValue(NumericTextBox.VisibilityProperty, _formatInfo.SecondFormat is null || isHiddenManual ? Visibility.Collapsed : Visibility.Visible);

                Grid.SetColumn(ToggleButtonT, GetPosition((_formatInfo.AmPmPosition ?? _defaultAmPmFormatPosition) + 1));

                // Fix positions which could be broken, because of AM/PM textblock.
                // Fix for seconds in a same way
                int dayPos = _formatInfo.DayPosition, monthPos = _formatInfo.MonthPosition, yearPos = _formatInfo.YearPosition,
                    hourPos = _formatInfo.HourPosition.Value, minutePos = _formatInfo.MinutePosition.Value, secondPos = _formatInfo.SecondPosition ?? 5,
                    amPmPos = _formatInfo.AmPmPosition ?? 6;
                FixNumericTextBoxesPositions(ref dayPos, ref monthPos, ref yearPos, ref hourPos, ref minutePos, ref secondPos, ref amPmPos);

                _textBoxes[dayPos]    = NumericTBDay;
                _textBoxes[monthPos]  = NumericTBMonth;
                _textBoxes[yearPos]   = NumericTBYear;
                _textBoxes[hourPos]   = NumericTBHour;
                _textBoxes[minutePos] = NumericTBMinute;
                _textBoxes[secondPos] = NumericTBSecond;
                _textBoxes[amPmPos]   = ListTBAmPm;

                // Fix tab order inside control.
                NumericTBDay.SetCurrentValue(TabIndexProperty, dayPos);
                NumericTBMonth.SetCurrentValue(TabIndexProperty, monthPos);
                NumericTBYear.SetCurrentValue(TabIndexProperty, yearPos);
                NumericTBHour.SetCurrentValue(TabIndexProperty, hourPos);
                NumericTBMinute.SetCurrentValue(TabIndexProperty, minutePos);
                NumericTBSecond.SetCurrentValue(TabIndexProperty, secondPos);
                ListTBAmPm.SetCurrentValue(TabIndexProperty, amPmPos);
                DatePickerIcon.SetCurrentValue(TabIndexProperty, Int32.MaxValue);

                SubscribeNumericTextBoxes();

                Separator1.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator1);
                Separator2.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator2);
                Separator3.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator3);
                Separator4.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator4);
                Separator5.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator5);
                Separator6.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator6);
                Separator7.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator7);
            }
            finally
            {
                _applyingFormat = false;
            }
        }
        private void ApplyFormat()
        {
            _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(Format, false);

            IsYearShortFormat = _formatInfo.IsYearShortFormat;
            NumericTBYear.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsYearShortFormat ? 0 : 1));
            NumericTBYear.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsYearShortFormat ? 99 : 3000));

            IsHour12Format = _formatInfo.IsHour12Format.Value;
            NumericTBHour.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsHour12Format.Value ? 1 : 0));
            NumericTBHour.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsHour12Format.Value ? 12 : 23));
            ToggleButtonH.SetCurrentValue(TagProperty, _formatInfo.IsHour12Format.Value ? DateTimePart.Hour12 : DateTimePart.Hour);

            IsAmPmShortFormat = _formatInfo.IsAmPmShortFormat.Value;

            EnableOrDisableYearConverterDependingOnFormat();
            EnableOrDisableHourConverterDependingOnFormat();
            EnableOrDisableAmPmConverterDependingOnFormat();

            ListTBAmPm.SetCurrentValue(ListTextBox.ListOfValuesProperty, new List <string>()
            {
                Meridiems.GetAmForFormat(_formatInfo),
                Meridiems.GetPmForFormat(_formatInfo)
            });

            NumericTBDay.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.DayFormat.Length));
            NumericTBMonth.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MonthFormat.Length));
            NumericTBYear.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.YearFormat.Length));
            NumericTBHour.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.HourFormat.Length));
            NumericTBMinute.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MinuteFormat.Length));
            NumericTBSecond.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.SecondFormat.Length));

            UnsubscribeNumericTextBoxes();

            Grid.SetColumn(NumericTBDay, GetPosition(_formatInfo.DayPosition));
            Grid.SetColumn(NumericTBMonth, GetPosition(_formatInfo.MonthPosition));
            Grid.SetColumn(NumericTBYear, GetPosition(_formatInfo.YearPosition));
            Grid.SetColumn(NumericTBHour, GetPosition(_formatInfo.HourPosition.Value));
            Grid.SetColumn(NumericTBMinute, GetPosition(_formatInfo.MinutePosition.Value));
            Grid.SetColumn(NumericTBSecond, GetPosition(_formatInfo.SecondPosition.Value));
            Grid.SetColumn(ListTBAmPm, GetPosition(_formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value));

            Grid.SetColumn(ToggleButtonD, GetPosition(_formatInfo.DayPosition) + 1);
            Grid.SetColumn(ToggleButtonMo, GetPosition(_formatInfo.MonthPosition) + 1);
            Grid.SetColumn(ToggleButtonY, GetPosition(_formatInfo.YearPosition) + 1);
            Grid.SetColumn(ToggleButtonH, GetPosition(_formatInfo.HourPosition.Value) + 1);
            Grid.SetColumn(ToggleButtonM, GetPosition(_formatInfo.MinutePosition.Value) + 1);
            Grid.SetColumn(ToggleButtonS, GetPosition(_formatInfo.SecondPosition.Value) + 1);
            Grid.SetColumn(ToggleButtonT, GetPosition(_formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value) + 1);

            // Fix positions which could be broken, because of AM/PM textblock.
            int dayPos = _formatInfo.DayPosition, monthPos = _formatInfo.MonthPosition, yearPos = _formatInfo.YearPosition,
                hourPos = _formatInfo.HourPosition.Value, minutePos = _formatInfo.MinutePosition.Value, secondPos = _formatInfo.SecondPosition.Value,
                amPmPos = _formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value;

            FixNumericTextBoxesPositions(ref dayPos, ref monthPos, ref yearPos, ref hourPos, ref minutePos, ref secondPos, ref amPmPos);

            _textBoxes[dayPos]    = NumericTBDay;
            _textBoxes[monthPos]  = NumericTBMonth;
            _textBoxes[yearPos]   = NumericTBYear;
            _textBoxes[hourPos]   = NumericTBHour;
            _textBoxes[minutePos] = NumericTBMinute;
            _textBoxes[secondPos] = NumericTBSecond;
            _textBoxes[amPmPos]   = ListTBAmPm;

            // Fix tab order inside control.
            NumericTBDay.SetCurrentValue(TabIndexProperty, dayPos);
            NumericTBMonth.SetCurrentValue(TabIndexProperty, monthPos);
            NumericTBYear.SetCurrentValue(TabIndexProperty, yearPos);
            NumericTBHour.SetCurrentValue(TabIndexProperty, hourPos);
            NumericTBMinute.SetCurrentValue(TabIndexProperty, minutePos);
            NumericTBSecond.SetCurrentValue(TabIndexProperty, secondPos);
            ListTBAmPm.SetCurrentValue(TabIndexProperty, amPmPos);
            DatePickerIcon.SetCurrentValue(TabIndexProperty, Int32.MaxValue);

            SubscribeNumericTextBoxes();

            Separator1.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator1);
            Separator2.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator2);
            Separator3.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator3);
            Separator4.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator4);
            Separator5.SetCurrentValue(TextBlock.TextProperty, Value == null || HideSeconds ? string.Empty : _formatInfo.Separator5);
            Separator6.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator6);
            Separator7.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator7);
        }
Exemple #4
0
            public void ReturnsCorrectDateFormatInfoForDateTimeFormat(string format, DateTimeFormatInfo expectedResult)
            {
                var result = DateTimeFormatHelper.GetDateTimeFormatInfo(format, false);

                Assert.That(AreEqual(result, expectedResult), Is.True);
            }
Exemple #5
0
        public static DateTimeFormatInfo GetDateTimeFormatInfo(string format, bool isDateOnly = false)
        {
            Argument.IsNotNull(() => format);

            var result = new DateTimeFormatInfo();
            var parts  = Split(format, new char[] { 'y', 'M', 'd', 'H', 'h', 'm', 's', 't' });

            var current = 0;
            var count   = 0;

            string hourFormat = null, hour12Format = null;

            foreach (string part in parts)
            {
                count++;

                if (count == 1 || count == parts.Length)
                {
                    if (!part.All(x => char.IsLetter(x)))
                    {
                        continue;
                    }
                }

                if (part.Contains('d'))
                {
                    if (result.DayFormat != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Day field can not be specified more than once");
                    }

                    if (part.Length > 2)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Day field must be in one of formats: 'd' or 'dd'");
                    }

                    result.DayFormat   = part;
                    result.DayPosition = current++;
                }
                else if (part.Contains('M'))
                {
                    if (result.MonthFormat != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Month field can not be specified more than once");
                    }

                    if (part.Length > 2)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Month field must be in one of formats: 'M' or 'MM'");
                    }

                    result.MonthFormat   = part;
                    result.MonthPosition = current++;
                }
                else if (part.Contains('y'))
                {
                    if (result.YearFormat != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Year field can not be specified more than once");
                    }

                    if (part.Length > 5)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Year field must be in one of formats: 'y' or 'yy' or 'yyy' or 'yyyy' or 'yyyyy'");
                    }

                    result.YearFormat   = part;
                    result.YearPosition = current++;
                }
                else if (part.Contains('h'))
                {
                    if (isDateOnly)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Time fields are not expected");
                    }

                    if (hourFormat != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Hour field must be 12 hour or 24 hour format, but no both");
                    }

                    if (hour12Format != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Hour field can not be specified more than once");
                    }

                    if (part.Length > 2)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Hour field must be in one of formats: 'h' or 'H' or 'hh' or 'HH'");
                    }

                    hour12Format        = part;
                    result.HourPosition = current++;
                }
                else if (part.Contains('H'))
                {
                    if (isDateOnly)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Time fields are not expected");
                    }

                    if (hour12Format != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Hour field must be 12 hour or 24 hour format, but no both");
                    }

                    if (hourFormat != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Hour field can not be specified more than once");
                    }

                    if (part.Length > 2)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Hour field must be in one of formats: 'h' or 'H' or 'hh' or 'HH'");
                    }

                    hourFormat          = part;
                    result.HourPosition = current++;
                }
                else if (part.Contains('m'))
                {
                    if (isDateOnly)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Time fields are not expected");
                    }

                    if (result.MinuteFormat != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Minute field can not be specified more than once");
                    }

                    if (part.Length > 2)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Minute field must be in one of formats: 'm' or 'mm'");
                    }

                    result.MinuteFormat   = part;
                    result.MinutePosition = current++;
                }
                else if (part.Contains('s'))
                {
                    if (isDateOnly)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Time fields are not expected");
                    }

                    if (result.SecondFormat != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Second field can not be specified more than once");
                    }

                    if (part.Length > 2)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Second field must be in one of formats: 's' or 'ss'");
                    }

                    result.SecondFormat   = part;
                    result.SecondPosition = current++;
                }
                else if (part.Contains('t'))
                {
                    if (isDateOnly)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Time fields are not expected");
                    }

                    if (result.AmPmFormat != null)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. AM/PM designator field can not be specified more than once");
                    }

                    if (part.Length > 2)
                    {
                        throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. AM/PM designator field must be in one of formats: 't' or 'tt'");
                    }

                    result.AmPmFormat   = part;
                    result.AmPmPosition = current++;
                }
                else
                {
                    if (current == 0)
                    {
                        result.Separator0 = part;
                    }
                    else if (current == 1)
                    {
                        result.Separator1 = part;
                    }
                    else if (current == 2)
                    {
                        result.Separator2 = part;
                    }
                    else if (current == 3)
                    {
                        result.Separator3 = part;
                    }
                    else if (current == 4)
                    {
                        result.Separator4 = part;
                    }
                    else if (current == 5)
                    {
                        result.Separator5 = part;
                    }
                    else if (current == 6)
                    {
                        result.Separator6 = part;
                    }
                    else if (current == 7)
                    {
                        result.Separator7 = part;
                    }
                }
            }
            result.HourFormat = hour12Format == null ? hourFormat : hour12Format;

            if (isDateOnly && (result.DayFormat == null || result.MonthFormat == null || result.YearFormat == null))
            {
                throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Day, month and year fields are mandatory");
            }

            if (!isDateOnly && (result.DayFormat == null || result.MonthFormat == null || result.YearFormat == null || result.HourFormat == null || result.MinuteFormat == null || result.SecondFormat == null))
            {
                throw Log.ErrorAndCreateException <FormatException>("Format string is incorrect. Day, month, year, hour, minute and second fields are mandatory");
            }

            result.IsYearShortFormat = result.YearFormat.Length < 3;

            if (!isDateOnly)
            {
                result.IsHour12Format    = result.HourFormat.Contains('h');
                result.IsAmPmShortFormat = result.AmPmFormat != null && result.AmPmFormat.Length < 2;
            }

            result.MaxPosition = current - 1;

            return(result);
        }
Exemple #6
0
        internal static string Format(DateTime dateTime, DateTimeFormatInfo formatInfo)
        {
            Argument.IsNotNull(() => formatInfo);

            var parts = new List <KeyValuePair <int, string> >();

            parts.Add(new KeyValuePair <int, string>(formatInfo.DayPosition, dateTime.Day.ToString(NumberFormatHelper.GetFormat(formatInfo.DayFormat.Length))));
            parts.Add(new KeyValuePair <int, string>(formatInfo.MonthPosition, dateTime.Month.ToString(NumberFormatHelper.GetFormat(formatInfo.MonthFormat.Length))));
            if (formatInfo.IsYearShortFormat)
            {
                var yearShort = dateTime.Year - (dateTime.Year - dateTime.Year % 100);
                parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, yearShort.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length))));
            }
            else
            {
                parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, dateTime.Year.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length))));
            }
            if (!formatInfo.IsDateOnly)
            {
                if (formatInfo.IsHour12Format == true)
                {
                    var hour12 = dateTime.Hour % 12 > 0 ? dateTime.Hour % 12 : 12;
                    parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, hour12.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length))));
                }
                else
                {
                    parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, dateTime.Hour.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length))));
                }
                parts.Add(new KeyValuePair <int, string>(formatInfo.MinutePosition.Value, dateTime.Minute.ToString(NumberFormatHelper.GetFormat(formatInfo.MinuteFormat.Length))));
                parts.Add(new KeyValuePair <int, string>(formatInfo.SecondPosition.Value, dateTime.Second.ToString(NumberFormatHelper.GetFormat(formatInfo.SecondFormat.Length))));
                if (formatInfo.AmPmPosition.HasValue)
                {
                    parts.Add(new KeyValuePair <int, string>(formatInfo.AmPmPosition.Value, dateTime.Hour >= 12 ? Meridiems.GetPmForFormat(formatInfo) : Meridiems.GetAmForFormat(formatInfo)));
                }
            }

            parts = parts.OrderBy(x => x.Key).ToList();

            // Always contain year, month, day part.
            var builder = new StringBuilder();

            builder.Append(formatInfo.Separator0);
            builder.Append(parts[0].Value);
            builder.Append(formatInfo.Separator1);
            builder.Append(parts[1].Value);
            builder.Append(formatInfo.Separator2);
            builder.Append(parts[2].Value);
            builder.Append(formatInfo.Separator3);

            if (parts.Count > 3) // Contain hour, minute, second part.
            {
                builder.Append(parts[3].Value);
                builder.Append(formatInfo.Separator4);
                builder.Append(parts[4].Value);
                builder.Append(formatInfo.Separator5);
                builder.Append(parts[5].Value);
                builder.Append(formatInfo.Separator6);

                if (parts.Count > 6) // Contain ampm part.
                {
                    builder.Append(parts[6].Value);
                    builder.Append(formatInfo.Separator7);
                }
            }

            return(builder.ToString());
        }