如何在NSUserDefaults.StandardUserDefaults中保存日期时间(日期)?

时间:2014-09-22 11:24:40

标签: c# ios xamarin.ios xamarin nsuserdefaults

或者除了将日期存储为字符串之外还有其他方法吗?

// 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");  

1 个答案:

答案 0 :(得分:4)

// 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);