question

davidbuckley-4911 avatar image
0 Votes"
davidbuckley-4911 asked WenyanZhang-MSFT commented

NSDate Function cant accept a null value

Is there a way to make this function accept a null datetime ?. By that I mean ToNSDate I have attempted it with my overide method but when I simply place Datetime? in an overide obv TotalSeconds does not exist.

 namespace Microsoft.Maui
 {
   public static class DateExtensions
   {
     internal static DateTime ReferenceDate = new DateTime(2001, 1, 1, 0, 0, 0);
    
     public static DateTime ToDateTime(this NSDate date)
     {
         return ReferenceDate.AddSeconds(date.SecondsSinceReferenceDate);
     }
              
     public static NSDate ToNSDate(this DateTime? date)
     {    
        return NSDate.FromTimeIntervalSinceReferenceDate((date - ReferenceDate).TotalSeconds);            
     }
             
      public static NSDate ToNSDate(this DateTime date)
      {  
        return NSDate.FromTimeIntervalSinceReferenceDate((date - ReferenceDate).TotalSeconds);           
      }
  }
 }

I called like this from the function

 public static void UpdateDate(this MauiDatePicker nativeDatePicker, IDatePicker datePicker, UIDatePicker? picker)
 {
             if (picker != null && picker.Date.ToDateTime().Date != datePicker.Date?.Date)
                 picker.SetDate(datePicker.Date.ToNSDate(), false);
                
             nativeDatePicker.Text = datePicker.Date?.ToString(datePicker.Format);
    
             nativeDatePicker.UpdateCharacterSpacing(datePicker);
 }



dotnet-csharpdotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Are you trying to figure out if "Date" is null before calling ToNSDate()???

 picker.SetDate(datePicker.Date?.ToNSDate(), false);

Null-conditional operators
null-coalescing operator


0 Votes 0 ·

No its not just about null its knowing what to pass to nsdate when date is null as not used to objective c

0 Votes 0 ·

No its not just about null its knowing what to pass to nsdate when date is null as not used to objective c

I can't wrap my head around the general logic. Can you explain the intent?

Date's type determines what extension method is executed. The NsDate type is the extension method's return type. You have not provider any of this information so we can only guess what you are trying to do.

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered WenyanZhang-MSFT commented

Try making a single function instead of two:

 public static NSDate ToNSDate( this DateTime? date )
 {
    if( date == null ) return null;
    
    return NSDate.FromTimeIntervalSinceReferenceDate( ( date.Value - ReferenceDate ).TotalSeconds );
 }

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

But NSDate does not understand what null is or does it ?

0 Votes 0 ·

How would I call this then from the picker.setdate function/

0 Votes 0 ·

You could have a try to use NSObject and cover the type with as.

 public override void ViewDidLoad()
         {
             base.ViewDidLoad();   
         UIDatePicker pickerView = new UIDatePicker(
             new CGRect(
                 0,
                 0,
                 UIScreen.MainScreen.Bounds.Width,
                 180
                 )
             );
         pickerView.Mode = UIDatePickerMode.DateAndTime;
         pickerView.SetDate(ToNSDate(null), false);// test null
         //pickerView.SetDate(ToNSDate(NSDate.DistantFuture),false);
         this.View.AddSubview(pickerView);
     }
     public static NSDate ToNSDate(NSObject date)
     {
         if (date == null) return NSDate.DistantPast;
         else
         {
             NSDate dateTime = date as NSDate;
             dateTime = NSDate.Now;
             return dateTime;
         }   
     }


0 Votes 0 ·