All of the Omni applications incorporate the use of dates, especially OmniFocus and OmniPlan. The following materials demonstrate how to generate and manipulate the JavaScript date objects that are used by Omni Automation to describe points in time.

Deriving Dates Relative to Date

A common practice in scripts is to create JavaScript date objects that represent future dates relative to an origin date, such as today.

The following details three commonly used techniques for deriving such date objects.

Using JavaScript

The JavaScript language has built-in methods for creating and manipulating instances of its Date class (date objects).

This example Omni Automation script uses the built-in JavaScript methods to generate a date object that references an entry that occurs on the 20th of next month at 1:00PM

// next month on the 20th at 1PM var d = new Date() var dateObj = new Date(d.getFullYear(), d.getMonth() + 1, 20, 13)

Using DateComponents Class

The DateComponents class and its functions are included in the Omni Automaton Calendar class, which is shared by all Omni applications.

Using DateComponents you can avoid the complex nested date calculations inherent in the built-in JavaScript date handling. The use of DateComponents may require more statements, but is clearer to visualize, as in the following example that generates a date object referencing a time three days from today at 10:30AM

// in three days at 10:46AM var now = new Date() var today = Calendar.current.startOfDay(now) var dc = new DateComponents = 3 dc.hour = 10 dc.minute = 46 dc.second = 0 var dateObj = Calendar.current.dateByAddingDateComponents(today, dc)

Using Formatter Class Date Interpreters

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:

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')