Working with Dates and Times
Workflow rules often depend on dates and times. You can notify users before a due date, create follow-up dates, calculate how long an issue has been in progress, or stop an update when a work item is logged for the wrong day.
In JavaScript workflows, most date and time values are represented as Unix timestamps in milliseconds. This means that values returned by Date.now(), date and date-time custom fields, and issue properties such as created, updated, and resolved can be compared as numbers.
Date and Time Values
The following values are commonly used in workflow rules:
Value | Description |
|---|---|
| Returns the current Unix timestamp in milliseconds. |
Issue properties | The |
Date custom fields | A custom field with the |
Date and time custom fields | A custom field with the |
Period custom fields | A custom field with the |
Work item dates | Work item dates are also available as timestamps. You can compare them with |
Use the corresponding field type in rule requirements when your rule reads or writes custom fields:
Compare Dates
Because date and date-time values are timestamps, you can compare them with relational operators. Always check that optional values are present before you compare them.
The following example checks whether a work item was added for a date more than one week in the past. Both values are normalized to the start of the UTC day before the comparison.
Parse and Format Values
The workflow API includes the @jetbrains/youtrack-scripting-api/date-time module. Use this module when you need to parse user-provided text, format timestamps for messages or search queries, or calculate dates by adding or subtracting periods.
Use dateTime.parse() instead of the standard JavaScript Date.parse() method. The workflow method lets you pass one or more expected formats and an optional time zone.
Use dateTime.format() to create readable dates for messages or dates in a specific format for search queries.
If you don't specify a format or time zone, YouTrack uses the values from the current user profile. For actions that are run by the workflow user, YouTrack uses the global date format and default time zone.
Add and Subtract Periods
Do not write expressions like Date.now() + 10 hours. JavaScript doesn't support period literals. Use numbers for fixed millisecond offsets or the date-time module for period-aware calculations.
For simple fixed intervals, define the interval in milliseconds:
For intervals that should be represented as periods, use dateTime.after() and dateTime.before(). Duration strings use a compact format with weeks, days, hours, and minutes.
You can also pass a period value that was read from a period custom field or created with dateTime.toPeriod().
Work with Period Fields
Period fields store values as period objects. To assign a value to a period field, convert a number of milliseconds or a period string with dateTime.toPeriod().
When you need to calculate with a period value, read its components with methods like getWeeks(), getDays(), getHours(), and getMinutes(). The following helper converts a period value to minutes using the common default of five working days per week and eight working hours per day:
If your YouTrack instance uses different time tracking settings, adjust the conversion to match your working week and working day.
Calculate Working Time
Use Project.intervalToWorkingMinutes() when you want to calculate the number of minutes that fall within the project's working hours. This is the method used by the default work timer workflows.
Use Issue.afterMinutes() when you need to add SLA-style business minutes to a starting timestamp. This method uses a calendar, such as the project's SLA calendar or the 24x7 calendar.
Tips
Use timestamps for direct comparisons and assignments to date or date-time fields.
Use
dateTime.parse()when a date comes from text input or an external service.Use
dateTime.format()before adding dates to comments, notifications, or search queries.Use
dateTime.toPeriod()before assigning values to period fields.Use
Project.intervalToWorkingMinutes()for work timer calculations that must respect working hours.Use
Issue.afterMinutes()for SLA-style calculations that must respect a business-hours calendar.