I code therefore I am.

April 7, 2010 at 6:32pm
2 notes
Comments (View)
home

Tags:
CSharp  Mono  

NSDate to DateTime and Back

  • Reference Date is January 1, 2001 0:00:00 UTC (GMT)
  • It’s important to note the Local Time Zone vs. UTC/GMT
NSDate to DateTime
public static DateTime NSDateToDateTime(NSDate date)
{
    DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime( 
        new DateTime(2001, 1, 1, 0, 0, 0) );
    return reference.AddSeconds(date.SecondsSinceReferenceDate);
}
DateTime to NSDate
public static NSDate DateTimeToNSDate(DateTime date)
{
    DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime(
        new DateTime(2001, 1, 1, 0, 0, 0) );
    return NSDate.FromTimeIntervalSinceReferenceDate(
        (date - reference).TotalSeconds);
}

blog comments powered by Disqus

Notes

  1. sourcerer posted this