void UpdateDateFromModel(bool animate) { if (_picker.Date.ToDateTime().Date != Element.Date.Date) { _picker.SetDate(Element.Date.ToNSDate(), animate); } // Can't use Element.Format because it won't display the correct format if the region and language are set differently if (string.IsNullOrWhiteSpace(Element.Format) || Element.Format.Equals("d", StringComparison.OrdinalIgnoreCase)) { NSDateFormatter dateFormatter = new NSDateFormatter(); dateFormatter.TimeZone = NSTimeZone.FromGMT(0); if (Element.Format?.Equals("D", StringComparison.Ordinal) == true) { dateFormatter.DateStyle = NSDateFormatterStyle.Long; var strDate = dateFormatter.StringFor(_picker.Date); Control.Text = strDate; } else { dateFormatter.DateStyle = NSDateFormatterStyle.Short; var strDate = dateFormatter.StringFor(_picker.Date); Control.Text = strDate; } } else if (Element.Format.Contains('/', StringComparison.Ordinal)) { Control.Text = Element.Date.ToString(Element.Format, CultureInfo.InvariantCulture); } else { Control.Text = Element.Date.ToString(Element.Format); } }
public static NSDateComponents ToNSDateComponents(this DateTimeOffset date) => new NSDateComponents() { Year = date.Year, Minute = date.Minute, Second = date.Second, Hour = date.Hour, Month = date.Month, Day = date.Day, TimeZone = NSTimeZone.FromGMT((nint)date.Offset.TotalSeconds) };
public void ToUtcDateTime_ForNsDate_ConvertsToUtcDateTime(double secondsSinceNow) { var nsDate = NSDate.FromTimeIntervalSinceNow(secondsSinceNow); var calendar = NSCalendar.CurrentCalendar; calendar.TimeZone = NSTimeZone.FromGMT(0); var utcComponents = CreateComponents(nsDate, calendar); var dateTime = nsDate.ToUtcDateTime(); Assert.IsType <DateTime>(dateTime); Assert.Equal(DateTimeKind.Utc, dateTime.Kind); Assert.Equal(utcComponents.Year, dateTime.Year); Assert.Equal(utcComponents.Month, dateTime.Month); Assert.Equal(utcComponents.Day, dateTime.Day); Assert.Equal(utcComponents.Hour, dateTime.Hour); Assert.Equal(utcComponents.Minute, dateTime.Minute); Assert.Equal(utcComponents.Second, dateTime.Second); }
public static void UpdateDate(this MauiDatePicker platformDatePicker, IDatePicker datePicker, UIDatePicker?picker) { if (picker != null && picker.Date.ToDateTime().Date != datePicker.Date.Date) { picker.SetDate(datePicker.Date.ToNSDate(), false); } string format = datePicker.Format ?? string.Empty; // Can't use VirtualView.Format because it won't display the correct format if the region and language are set differently if (picker != null && string.IsNullOrWhiteSpace(format) || format.Equals("d", StringComparison.OrdinalIgnoreCase)) { NSDateFormatter dateFormatter = new NSDateFormatter { TimeZone = NSTimeZone.FromGMT(0) }; if (format.Equals("D", StringComparison.Ordinal) == true) { dateFormatter.DateStyle = NSDateFormatterStyle.Long; var strDate = dateFormatter.StringFor(picker?.Date); platformDatePicker.Text = strDate; } else { dateFormatter.DateStyle = NSDateFormatterStyle.Short; var strDate = dateFormatter.StringFor(picker?.Date); platformDatePicker.Text = strDate; } } else if (format.Contains('/', StringComparison.Ordinal)) { platformDatePicker.Text = datePicker.Date.ToString(format, CultureInfo.InvariantCulture); } else { platformDatePicker.Text = datePicker.Date.ToString(format); } platformDatePicker.UpdateCharacterSpacing(datePicker); }