private void UpdateComponents() { var culture = Language.GetSpecificCulture(); var dateTimeFormatInfo = culture.GetDateFormat(); var minDateTime = dateTimeFormatInfo.Calendar.MinSupportedDateTime; var maxDateTime = dateTimeFormatInfo.Calendar.MaxSupportedDateTime; if (DisplayDate < minDateTime) { SetDisplayDateOfCalendar(minDateTime); // return to avoid second formatting of the same value return; } if (DisplayDate > maxDateTime) { SetDisplayDateOfCalendar(maxDateTime); // return to avoid second formatting of the same value return; } var calendarFormatInfo = CalendarFormatInfo.FromCultureInfo(culture); var displayDate = DisplayDate; ComponentOneContent = FormatDate(calendarFormatInfo.ComponentOnePattern, displayDate, culture); ComponentTwoContent = FormatDate(calendarFormatInfo.ComponentTwoPattern, displayDate, culture); ComponentThreeContent = FormatDate(calendarFormatInfo.ComponentThreePattern, displayDate, culture); }
/// <summary> /// Creates a <see cref="CalendarFormatInfo"/> from the <see cref="CultureInfo"/>. /// </summary> /// <param name="cultureInfo">A <see cref="CultureInfo"/> that specifies the culture to get the date format.</param> /// <returns>The <see cref="CalendarFormatInfo"/> object that this method creates.</returns> /// <exception cref="ArgumentNullException"><paramref name="cultureInfo"/> is null.</exception> public static CalendarFormatInfo FromCultureInfo(CultureInfo cultureInfo) { if (cultureInfo is null) { throw new ArgumentNullException(nameof(cultureInfo)); } CalendarFormatInfo calendarInfo; if (_formatInfoCache.TryGetValue(cultureInfo.Name, out calendarInfo)) { return(calendarInfo); } var dateTimeFormat = cultureInfo.DateTimeFormat; string yearPattern; if (!_cultureYearPatterns.TryGetValue(cultureInfo.Name, out yearPattern)) { yearPattern = "yyyy"; } DayOfWeekStyle dayOfWeekStyle; if (!_cultureDayOfWeekStyles.TryGetValue(cultureInfo.Name, out dayOfWeekStyle)) { dayOfWeekStyle = DayOfWeekStyle.Parse(dateTimeFormat.LongDatePattern); } var monthDayPattern = dateTimeFormat.MonthDayPattern.Replace("MMMM", "MMM"); if (dayOfWeekStyle.IsFirst) { calendarInfo = new CalendarFormatInfo(dateTimeFormat.YearMonthPattern, monthDayPattern, dayOfWeekStyle.Pattern + dayOfWeekStyle.Separator, yearPattern); } else { calendarInfo = new CalendarFormatInfo(dateTimeFormat.YearMonthPattern, dayOfWeekStyle.Pattern, monthDayPattern + dayOfWeekStyle.Separator, yearPattern); } _formatInfoCache[cultureInfo.Name] = calendarInfo; return(calendarInfo); }