Deriving Dates Relative to Date
The following details three commonly used techniques for deriving such date objects.
The DateComponents class and its functions are included in the Omni Automaton Calendar class, which is shared by all Omni applications.
// in three days at 10:46AM
var now = new Date()
var today = Calendar.current.startOfDay(now)
var dc = new DateComponents
dc.day = 3
dc.hour = 10
dc.minute = 46
dc.second = 0
var dateObj = Calendar.current.dateByAddingDateComponents(today, dc)
In Omni applications, date entry fields support the entering of shortcut date terminology, such as the words: “today” or “tomorrow” or shorthand values like: “+3d” or “3 days”
The value for a date input field can written using any syntax supported by an OmniOutliner date field. For example:
2d, –3w, 1h, 1y1m, and so on • Relative dates and times put the date at a certain amount of time from right now. Negative numbers represent times in the past.
2 days, –3 weeks, 1 hour, 1 year 1 month, and so on • You can use the full names of units too.
yesterday, tomorrow, tonight, next thursday, last month, this friday, and so on • You can refer to relative dates using common words. “This”, “next”, and “last” have specific meanings: this friday always means the Friday in this week, next friday always means the Friday in the next week, and last friday always means the Friday in last week, regardless of what day today is. Other units work in the same way.
september, fri, 2019, and so on • If you enter the name of a specific time period, the date will be at its beginning. So september means September first.
5/23/08 10a, 9.30.09 2:00 PM, and so on • You can use the short date format as defined in your Language & Region system preferences.
2w sat, 4d @ 5p, mon 6a, aug 6 tue 5p, and so on • Mix the available formats however you like.
now, 9, 14:00, tom, and so on • Omni’s date parser makes its best guess at things like bare numbers, times, and word fragments.
Using an instance of the shared Formatter.Date class, you can convert those supported shorthand terms into date objects for use in scripts!
For example, the following script shows how to create a date object for three days from today at 9:30AM
// in three days at 9:30AM
var fmatr = Formatter.Date.withStyle(Formatter.Date.Style.Short)
var dateObj = fmatr.dateFromString('3d @ 9:30a')