NSDateFormatter for today and yesterday

Is there a formatter to display a date like this:

Today at 9:34:47
or
Yesterday at 16:28:19

I found the isDateInToday: parameter of the NSCalendar class.
But my question is still valid: is there a shorter way to do this?

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set theDate to ((current date) - 1 * days)
set theCal to current application's NSCalendar's currentCalendar()
if theCal's isDateInToday:theDate then
	set theDay to "Aujourd'hui"
else if theCal's isDateInYesterday:theDate then
	set theDay to "Hier"
else
	set theFormatter to "EEEE d MMMM"
	set dateFormat to current application's NSDateFormatter's alloc()'s init()
	dateFormat's setDateFormat:theFormatter
	set theDay to (dateFormat's stringFromDate:theDate) as text
end if

set theFormatter to "HH:mm:ss"
set dateFormat to current application's NSDateFormatter's alloc()'s init()
dateFormat's setDateFormat:theFormatter
set theHour to (dateFormat's stringFromDate:theDate) as text

set theString to theDay & " à " & theHour

What you’re looking for is relative date formatting:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
set theDate to ((current date) - 1 * days)
set dateFormat to current application's NSDateFormatter's alloc()'s init()
dateFormat's setDateStyle:(current application's NSDateFormatterLongStyle)
dateFormat's setTimeStyle:(current application's NSDateFormatterMediumStyle)
dateFormat's setDoesRelativeDateFormatting:true
set theDay to (dateFormat's stringFromDate:theDate) as text

Great!
That’s exactly what I was looking for.

Yet the doesRelativeDateFormatting: property is on the NSFormatter class page!

:wink:

Are you sure? It’s under NSDateFormatter here…

Maybe my english is wrong:
I was trying to say that the property is on the page and I’ve missed it!

(In french, it should be: “Pourtant, la propriété doesRelativeDateFormatting: est sur la page.”)