15

I am trying to take a date string and turn it into a specific NSDate (eg. July 1, 1981), but I don't see and methods for setting the date. Does anyone know how to accomplish this? Perhaps convert a DateTime object to NSDate?

Bryan
  • 17,201
  • 24
  • 97
  • 123

5 Answers5

26

The easiest way is to set it from DateTime.

REVISION: The NSDate conversion operators are now explicit, not implicit anymore! I updated the example below.

If you look at the NSDate prototype you will find two operators:

    public static explicit operator NSDate(DateTime dt);
    public static explicit operator DateTime(NSDate d);

These two will do the conversion for you.

Explicit conversion of NSDate to and from DateTime is quite good, but you must be aware that NSDate is always an UTC time and DateTime is default set to DateTimeKind.Unspecified (when read from database) or DateTimeKind.Locale (when set with DateTime.Today). The best way to convert without complicated time-zone computations is to force the right DateTimeKind:

    // Set NSDate:
    DateTime date = DateTime.Parse("1981-07-01")
    NSDate nsDate = (NSDate)DateTime.SpecifyKind(date, DateTimeKind.Utc);

    // Get DateTime from NSDate:
    date = DateTime.SpecifyKind((DateTime)nsDate, DateTimeKind.Unspecified);
Marcel Wolterbeek
  • 3,367
  • 36
  • 48
  • 4
    I would recommend adding a the following line to make sure your converting properly `date = date.ToLocalTime ();` worked perfectly for me in addition to what you have mentioned. Thanks for the guidance. – BRogers Mar 15 '13 at 20:27
  • `date = DateTime.SpecifyKind(nsDate, DateTimeKind.Unspecified);` This for me gives the incorrect date – gyozo kudor Nov 03 '14 at 07:04
  • I don't understand how nsDate = DateTime.SpecifyKind works. DateTime.SpecifyKind returns a date, not NSDate. – Victor Chelaru May 15 '15 at 19:56
  • @VictorChelaru: Conversion is done using the operators in NSDate. They used to be implicit, but in the new Xamarin api they are explicit. I updated the answer. – Marcel Wolterbeek May 19 '15 at 17:23
  • @MarcelW: Which solution is the better one? Yours or the explicit conversion [described here](http://developer.xamarin.com/guides/cross-platform/macios/unified/#Converting_DateTime_to_NSDate)? What is the difference to your approach? – testing Aug 14 '15 at 09:16
9
public static DateTime NSDateToDateTime(MonoTouch.Foundation.NSDate date)
{
    return (new DateTime(2001,1,1,0,0,0)).AddSeconds(date.SecondsSinceReferenceDate);
}

public static MonoTouch.Foundation.NSDate DateTimeToNSDate(DateTime date)
{
    return MonoTouch.Foundation.NSDate.FromTimeIntervalSinceReferenceDate((date-(new DateTime(2001,1,1,0,0,0))).TotalSeconds);
}

This should help

Alex

dalexsoto
  • 3,412
  • 1
  • 27
  • 43
  • 4
    This doesn't take into account timezone differences between how NSDate handles stuff and how datetime does. – BRogers Mar 15 '13 at 19:39
  • 1
    http://developer.xamarin.com/guides/cross-platform/macios/unified/#Converting_DateTime_to_NSDate – testing Aug 14 '15 at 09:00
2

Try this:

private static NSDate DateTimeToNSDate(DateTime date)
    {
        NSCalendar calendar = NSCalendar.CurrentCalendar;
        NSDateComponents comps = new NSDateComponents();
        comps.Day = date.Day;
        comps.Month = date.Month;
        comps.Year = date.Year;
        comps.Minute = date.Minute;
        comps.Hour = date.Hour;

        return calendar.DateFromComponents(comps);
    }
tntwist
  • 71
  • 5
0
public NSDate ConvertDateTimeToNSDate(DateTime date)
{
    DateTime newDate = TimeZone.CurrentTimeZone.ToLocalTime(
        new DateTime(2001, 1, 1, 0, 0, 0) );
    return NSDate.FromTimeIntervalSinceReferenceDate(
        (date - newDate).TotalSeconds);
}

public DateTime ConvertNsDateToDateTime(NSDate date)
{
    DateTime newDate = TimeZone.CurrentTimeZone.ToLocalTime( 
        new DateTime(2001, 1, 1, 0, 0, 0) );
    return newDate.AddSeconds(date.SecondsSinceReferenceDate);
}

Source : https://theweeklybyte.wordpress.com/2014/05/27/convert-datetime-to-nsdate-and-back/

cegprakash
  • 2,937
  • 33
  • 60
-2

You want to look at the NSDateFormatter class :)

Your code will look something like this . . .

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM dd, yyyy HH:mm"];

NSDate *parsed = [formatter dateFromString:dateString];

S

PS Example liberally copied from this question :)

Community
  • 1
  • 1
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • I will look into setting the date using the NSDateFormatter. Monotouch does not have all of the methods of objective C, but it look like a step in the right direction. Thanks Dean – Bryan Jan 27 '10 at 17:32
  • I've never used monotouch myself but apparently NSDateFormatter is available in version 1.4.4 (http://monotouch.net/index.php?title=Releases/MonoTouch_1.4.4&highlight=NSDateFormatter). – deanWombourne Jan 27 '10 at 17:47