testing testing - 1 month ago 11
C# Question

How to save a datetime (date) in NSUserDefaults.StandardUserDefaults?

Or there other ways than storing a date as string?

// read date
string dateString = NSUserDefaults.StandardUserDefaults.StringForKey ("MY_DATE");
myDate = new DateTime ();
myDate = DateTime.ParseExact (dateString, "dd.MM.yyyy", null);

// write date
NSUserDefaults.StandardUserDefaults.SetString (myDate.Value.ToString ("dd.MM.yyyy"), "MY_DATE");

Answer
// key
NSString k = new NSString ("key");

// store a date
NSDate val = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Utc);
NSUserDefaults.StandardUserDefaults.SetValueForKey (val, k);

// retrieve a date
NSDate nsdate = (NSDate) NSUserDefaults.StandardUserDefaults.ValueForKey (k);
DateTime date = DateTime.SpecifyKind(nsdate, DateTimeKind.Unspecified);