Is there a way to get a formatted date interval (not duration)

I am writing an OmniFocus plug-in and want to format a date interval as a string.

I can get the start and end dates from Form.Field.Date and want a string like:

Jan 3 - 4, 2024

If it spans months:

Jan 3 - Feb 4, 2024

If it spans years:

Dec 3, 2023 - Jan 4, 2024

In Foundation, DateIntervalFormatter (in Objective-C NSDateIntervalFormatter) does exactly what I want, using the ‘medium’ date style.

So two related questions:

1. Is there a way to get a string formatted like this using the OmniFocus Automation API?
I found the date and duration formatters. I also found the DateRange type in the OmniFocus API, but it does not seem to have a constructor and its properties are all read-only.

2. If there is no way to do this with the existing OmniFocus Automation API, is there a way to write a plug-in that calls Objective-C or Swift code?

I realize I could write code that compares date components, builds the correct string, etc. but would rather use an existing, tested formatter that already does what I need, if at all possible.

Thanks for any help in this.

Could you post a minimal working example and a rough idea about the use case ?

The use case is I often have trips to the same destination, the main differentiator are the travel dates.

So I manually have projects for each trip like “New Jersey, Dec 4 - 8, 2023”.

I want to pick the start and end dates using an automation form, which would allow me to do things like automatically defer trip follow-up tasks until after the end date. I’d also want to automatically generate the date string that is part of the trip project name.

I’m trying to avoid spending the time to create a minimal working example, which is why I’m asking the questions in the original post.

I ended up writing this to get the formatting I wanted.

It isn’t terribly long, but unlike using DateIntervalFormatter it is not localized and makes assumptions about the format of Formatter.Date.Style.Medium to work correctly.

I’ve also sent feedback in to consider adding DateInterval and DateIntervalFormatter to the Omni Automation API.

let outputFormatter = Formatter.Date.withStyle(Formatter.Date.Style.Medium, Formatter.Date.Style.None)
let startString = outputFormatter.stringFromDate(startDate)
let endString = outputFormatter.stringFromDate(endDate)
let separatorString = " - "
		
let spanString = ""
if (startComponents.year != endComponents.year) {
    spanString = startString + separatorString + endString
} else if (startComponents.month != endComponents.month) {
    let startValue = startString.slice(0, startString.indexOf(","))
    spanString = startValue + separatorString + endString
} else if (startComponents.day != endComponents.day){
    let commaIndex = startString.indexOf(",")
    let startValue = startString.slice(0, commaIndex)
    let endValue = endString.slice(4) // Assumes 3 character month + space
    spanString = startValue + separatorString + endValue
} else {
    spanString = startString
}