Jeff is right, NSCoding is probably the preferred way to serialize NSDate objects. Anyway, if you really want / want to save the date as a simple date string, this can help you:
Actually, NSDateFormatter not limited to predefined formats. You can set an arbitrary arbitrary format using the dateFormat property. The following code should be able to parse date strings in an "international format", i.e. NSDate -description format uses:
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZZ"; NSDate* date = [dateFormatter dateFromString:@"2001-03-24 10:45:32 +0600"];
For a complete reference to the format string syntax, check out the Unicode standard .
However, be careful with -description - the output of these methods is usually aimed at people readers (for example, log messages), and it is not guaranteed that it will not change its output format in the new version of the SDK! You must use the same date format to serialize your date object:
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZZ"; NSString* dateString = [dateFormatter stringFromDate:[NSDate date]];
Daniel Rinser
source share