Tyson
Tyson

Reputation: 14734

Getting an NSDateComponents instance from an NSDate (Or DateTime) in MonoTouch

I need to get a fully defined NSDateComponents instance (i.e. with calender, timezone, and the actual date values etc set) to be used as the StartDateComponents and DueDateComponents values of an EKReminder.

There are plenty of answers here on SO explaining how to do it from a calendar instance in obj-c. And open source projects doing the same thing.

However I can't seem to work out how to do the same thing in MT. The NSCalendar class doesn't expose any method related to the components:fromDate: selector.

How have people dealt with DateTime/NSDate -> NSDateComponents in MT?

Upvotes: 2

Views: 767

Answers (1)

Rolf Bjarne Kvinge
Rolf Bjarne Kvinge

Reputation: 19345

These API are effectively missing from MonoTouch - but I've now added them so you'll will find them in a future release.

In the meantime you can call components:fromDate: manually:

var calendar = new NSCalendar (NSCalendarType.Gregorian);
var date = (NSDate) DateTime.Now;
var ptr = Messaging.IntPtr_objc_msgSend_UInt32_IntPtr (calendar.Handle, Selector.GetHandle ("components:fromDate:"), (uint) (NSCalendarUnit.Year | NSCalendarUnit.Month), date.Handle);
var comps = new NSDateComponents (ptr);

Console.WriteLine (comps.Year);

Upvotes: 2

Related Questions