NSFormatter dateFromString Problem?

I have a program where I store a date as a string in a plist, and then convert it back to a date, format it and then bind it to a text field. One of the ways I tried to do this was using a date formatter’s dateFromString method. This method works if I pass it a literal string like “12/25/2010”, but if I pass it the string derived from NSDate’s date()'s |description|() it doesn’t work. Here is the code:

script TimeStampingAppDelegate
	property parent : class "NSObject"
	property TimeConversions : class "TimeConversions"
	on applicationWillFinishLaunching_(aNotification)
		--TimeConversions's doConversions()
		set lastOpenedDate to current application's NSDate's |date|()
		log lastOpenedDate
		log lastOpenedDate's |class|()
		set lastOpenedString to lastOpenedDate's |description|()
		log lastOpenedString
		log lastOpenedString's |class|()
		set dFormatter to current application's NSDateFormatter's alloc()'s init()
		dFormatter's setDateStyle_(current application's NSDateFormatterShortStyle)
		dFormatter's setTimeStyle_(current application's NSDateFormatterShortStyle)
		dFormatter's setDoesRelativeDateFormatting_(1)
		log dFormatter's dateFromString_(lastOpenedString)
		log dFormatter's stringFromDate_(lastOpenedDate)
	end applicationWillFinishLaunching_
end script

And here are the results:

Am I doing something wrong here, or is this a bug? I also tried this in Objective C, and I get the same results there.


The docs say dateFromString: “Returns a date representation of a given string interpreted using the receiver’s current settings.” You’re not passing in a string formatted with the receiver’s current settings – you’re passing one that is formatted with fixed settings. You would probably have to use descriptionWithCalendarFormat:timeZone:locale: to get the matching format.

But why not just save the NSDate directly in the property list?

I thought that meant that the formatter would create a date formatted to the formatter’s settings, from any string that it could interpret.

Because I mis-remembered what you could store directly in plists, and thought you couldn’t do that. Thanks for straightening me out on that one – it makes the code a lot simpler.


dateFromString is very particular. I tried to use it as a way of getting around the failure of date as an AS specifier in ASObjC, and ran into all sorts of strife.


NSDateFormatter is no magic, the “receiver’s current settings” depend on the individual date format settings in Language pref pane.
My current settings (swiss german) running this ObjC code are

dFormatter = [[NSDateFormatter alloc] init];
	[dFormatter setDateStyle:NSDateFormatterShortStyle];
	[dFormatter setTimeStyle:NSDateFormatterShortStyle];
	NSLog(@"dateFormat: %@", [dFormatter dateFormat]); // dateFormat: dd.MM.yy HH:mm
	NSLog(@"locale: %@", [[dFormatter locale] localeIdentifier]); // locale: de_CH
	NSLog(@"dateStyle: %d", [dFormatter dateStyle]); // dateStyle: 1 (ShortStyle)
	NSLog(@"timeStyle: %d", [dFormatter timeStyle]); // timeStyle: 1 (ShortStyle)
	NSLog(@"timeZone: %@", [dFormatter timeZone]); // timeZone: Europe/Zurich (GMT+01:00) offset 3600
	[dFormatter release];

MediumStyle results dd.MM.yyyy HH:mm:ss
LongStyle results d. MMMM yyyy HH:mm:ss z
FullStyle results EEEE, d. MMMM yyyy HH:mm:ss v

The dateFromString method expects the format pattern specified in the dateFormat property.

The default date and time style is NSDateFormatterNoStyle, in this case the dateFormat pattern is an empty string.

If you want an exact format, don’t use the style but set the dateFormat property

For the format patterns see http://unicode.org/reports/tr35/tr35-6.html#Number_Format_Patterns

See also Introduction to Data Formatting Programming Guide For Cocoa