Working with Periods
Period values represent amounts of time, such as 3h, 2d, or 1w3d4h. In workflows, you usually work with period values when you read or update fields like Estimation and Spent time, add work items, or calculate dates from a duration.
A field that stores the period type returns a period object. To create a period object in workflow code, use the date-time module. To compare period values, convert them to a common unit first.
Period String Syntax
The date-time module accepts period strings with numeric values followed by unit abbreviations. Specify units in descending order, without spaces.
Unit | Description | Example |
|---|---|---|
| Weeks |
|
| Days |
|
| Hours |
|
| Minutes |
|
For example, 2w4d3h15m represents two weeks, four days, three hours, and fifteen minutes.
The way period values are displayed in YouTrack depends on the period format and time tracking settings for the instance. The compact string format is the format you use when you create period values in workflow code.
Create Period Values
Use dateTime.toPeriod() to convert a period string or a number of milliseconds to a period object.
When you write a value to a period custom field, assign a period object, not a plain string.
Read Period Fields
Read a period field from issue.fields. Optional fields can be empty, so check that the value exists before you use it.
To show the value in the same format that YouTrack uses for the issue, use getValuePresentation() from the field requirement.
Convert Periods
Period objects are not numbers. When you need to compare, add, or subtract period values, convert them to a common unit first.
The following helper converts a period value to minutes. It uses a five-day work week and an eight-hour work day, which are common default time tracking settings.
If your YouTrack instance uses different time tracking settings, adjust the constants to match your working week and working day.
After you calculate a new value in minutes, convert it back to a period before assigning it to a period field.
Compare Period Values
To compare two period fields, convert both values to the same unit. The following rule prevents users from increasing Spent time beyond the current estimation.
Use Periods with Dates
Use dateTime.after() and dateTime.before() when a period should move a timestamp forward or backward.
These methods return Unix timestamps in milliseconds. Assign the result to a date or date and time field, or compare it with other timestamp values.
Calculate Work Item Duration
Work item durations are stored as minutes. When a workflow records time between two timestamps, use Project.intervalToWorkingMinutes() to calculate the number of minutes that fall within the project's working hours.
This is the same approach used by the default work timer workflows. To store a calculated duration in a period field instead, convert the number of minutes to milliseconds and pass it to dateTime.toPeriod().
Tips
Use
dateTime.toPeriod()before assigning values to period fields.Use compact period strings like
3hand2w4d3h15min workflow code.Use
getWeeks(),getDays(),getHours(), andgetMinutes()when you need to calculate with a period value.Convert period values to minutes before you compare them.
Use
dateTime.after()anddateTime.before()to calculate dates from periods.Use
Project.intervalToWorkingMinutes()when a duration should respect project working hours.