Time Stamps

Does anyone know whether there is an easy way to get time stamps like you get in Mail or in the Xcode welcome window, where they include things like “Today 9:05 PM” or “Yesterday 3:47 AM” , and then go to dates for further back in time than yesterday?

Ric

Use an NSDateFormatter with setDoesRelativeDateFormatting:.

Shane,

Thanks for the suggestion. I can get the relative date formatting now, but I don’t seem to be able to get both the date and time.

This is the code I have:

script TimeStampingAppDelegate
	property parent : class "NSObject"
	property NSDateFormatter : class "NSDateFormatter"
	property tf : missing value --bound to the text field's value
	property fieldCell : missing value --outlet to the texField cell
	
	on applicationWillFinishLaunching_(aNotification)
		set dFormatter to NSDateFormatter's alloc()'s init()
		dFormatter's setFormatterBehavior_(current application's NSDateFormatterBehavior10_4)
		dFormatter's setDoesRelativeDateFormatting_(1)
		dFormatter's setTimeStyle_(current application's NSDateFormatterShortStyle)
		dFormatter's setDateStyle_(current application's NSDateFormatterShortStyle)
		fieldCell's setFormatter_(dFormatter)
		setTf_(current application's NSDate's |date|())
	end applicationWillFinishLaunching_
	applicationWillFinishLaunching_
	
end script

With the code as is, I get nothing in the text field. If I comment out either the setDateStyle or setTimeStyle statements, then I can get either the time or the date. The culprit does seem to be the setDoesRelativeDateFormatting statement --if I comment that out, then I can get both the date and time.

Ric

After Edit: I still don’t know why this doesn’t work, but I found a way around it. If I don’t attach the DateFormatter to the text field cell, and instead, use dFormatter’s stringFromDate_, and then bind that string to the text field, it works.

You’re not running out of space in the cell or something?

This works fine here:

	on applicationWillFinishLaunching_(aNotification)
		set dFormatter to current application's NSDateFormatter's alloc()'s init()
		tell dFormatter
			setFormatterBehavior_(current application's NSDateFormatterBehavior10_4)
			setDoesRelativeDateFormatting_(true)
			setDateStyle_(current application's NSDateFormatterShortStyle)
			setTimeStyle_(current application's NSDateFormatterShortStyle)
			log stringFromDate_(current application's NSDate's |date|())
		end tell
	end applicationWillFinishLaunching_

Shane,

What you did, was what I did for my work around in my after edit comment. If you send the formatter the stringFromDate command, and then bind that string to the text field it works. But, if you attach the formatter to the text field cell and pass it NSDate’s date(), it doesn’t work. The work around is simple enough, but shouldn’t it work the other way as well?

Ric

I’m confused. I just added a text field and did a copy-and-paste of your script, and it works as expected.

Well, I’m doubly confused --I just copied and pasted my posted script into a new program, and it didn’t work as expected. It did exactly what I said in my first post. I’m not sure why we should get different results (I’m using Xcode version 3.2.5 on OS 10.6.5).

Ric

just a side note:
Since 10.5 NSDateFormatterBehavior10_4 is the default behavior, so the line can be omitted in every ASOC environment

False (non) alarm – I’m getting the same as you. I tried using it as a Defined Runtime Attribute, too, but still no luck. I also tried skipping bindings to see if that was the problem, but no. And adding the formatter in IB and only modifying it in code, but again no. Looks like it only works with strings directly.

Actually, doing it all in IB still fails.