Converting a Duration Number/String to a Moment.js duration object
You can convert a duration number/string to a Moment.js duration object using the duration method of the Moment.js library. For example:
3 | duration("minutes")
returns a Moment.js duration object.
When rendered in a {{...}} block, a Moment.js duration object is output as a string representing the specified duration in milliseconds.
{{
outputs 1800003 | duration("minutes")
}}
{{ "2:30:00" | duration }} outputs 9000000
{days:2,hours:12} | duration
Converting a Moment.js duration object to a humanized string
You can convert a Moment.js duration object to a string representing a duration using the duration("humanize")
filter. For example:
{{
returns the current date/time as an ISO 8601-compliant string such as 3 | duration("minutes")
| duration("humanize") }}2018-02-14T00:00:00Z
.
To format a Moment.js date object to a more human-readable string, use the date(format)
form of the filter.
Duration calculations
You might want to manipulate a duration before setting it to a field. For example, you might want to set the Original estimate to the difference of the Created and Due date. Below are some methods to help with these manipulations.
add
Description:
Adds time to a Momentjs duration.
Syntax:
{{ durationObject | duration('add' , amount [, unit ] ) }}
Parameters:
Parameter | Description |
---|---|
amount | The amount of time to add. Can be a number or another Moment.js duration object |
unit | The unit of the amount parameter. One of seconds , minutes , hours , days , weeks , months , years . If not specified, and the amount parameter is a number, it will be interpreted as a number of milliseconds. The shorthand keys are: 'y', 'Q', 'M', 'w', 'd', 'h', 'm', 's' and 'ms' for years, quarter, months, weeks, days, hours, minutes, seconds and milliseconds respectively. |
Examples:
Input | Details | Output |
---|---|---|
| The String can be 'days' or shorthand 'd' | 777600000 |
{{ 3 | duration("hours") | duration("add", -6, "hours") }} | The String can be 'hours' or shorthand 'h' | -10800000 |
{{ 5 | date('add' , {months:2, days:3}) | duration ("months") }} | You can also pass them in object literal | 7 |
subtract
Description:
Subtracts time from a Momentjs duration.
Syntax:
{{ durationObject | duration('subtract' , amount [, unit ] ) }}
Parameters:
Parameter | Description |
---|---|
amount | The amount of time to subtract. Can be a number or another Moment.js duration object |
unit | The unit of the amount parameter. One of seconds , minutes , hours , days , weeks , months , years . If not specified, and the amount parameter is a number, it will be interpreted as a number of milliseconds. The shorthand keys are: 'y', 'Q', 'M', 'w', 'd', 'h', 'm', 's' and 'ms' for years, quarter, months, weeks, days, hours, minutes, seconds and milliseconds respectively. |
Examples:
Input | Details | Output |
---|---|---|
| The String can be 'days' or shorthand key 'd' | -259200000 |
{{ 5 | date('subtract' , {months:2, days:3}) | duration ("months") }} | You can also pass them in object literal | 2016-06-04T08:51:47Z |
get
Description:
Returns a part of the Moment.js duration (milliseconds, seconds, minutes, ...)
Syntax:
{{ durationObject | duration([unit]) }}
Parameters:
Parameter | Description |
---|---|
unit | The unit of the number to return. One of seconds , minutes , hours , days , weeks , months , years . The shorthand keys are: 'y', 'Q', 'M', 'w', 'd', 'h', 'm', 's' and 'ms' for years, quarter, months, weeks, days, hours, minutes, seconds and milliseconds respectively. |
Examples:
Input | Output |
---|---|
| 3 |
{{ "2:30:00" | duration | duration("get", "minutes") }} | 30 |
as
Description:
Converts a Moment.js duration into a number of milliseconds, or seconds, or minutes, ...
Syntax:
{{ durationObject | duration([unit]) }}
Parameters:
Parameter | Description |
---|---|
unit | The unit of the number to return. One of seconds , minutes , hours , days , weeks , months , years . The shorthand keys are: 'y', 'Q', 'M', 'w', 'd', 'h', 'm', 's' and 'ms' for years, quarter, months, weeks, days, hours, minutes, seconds and milliseconds respectively. |
Examples:
Input | Output |
---|---|
| 180 |
{{ | 150 |
Duration formatting
You might want to display a duration in a specific format after or before manipulating it. Momentjs provides some methods to display a duration in different formats.
humanize
Description:
Formats a Moment.js duration object into a user-friendly string. To format it in a specific locale (language), use theduration("locale")
filter first. It applies to aMoment.js durationobject, as returned by theduration([unit])
filter.
Syntax:
{{ durationObject | duration("humanize" [,relative]) }}
Parameters:
Parameter | Description | |
---|---|---|
amount | The amount of time to subtract. Can be a number or another Moment.js duration object | |
unit | Parameter | Description |
amount | The amount of time to subtract. Can be a number or another Moment.js duration object | |
unit | The unit of the amount parameter. One of seconds , minutes , hours , days , weeks , months , years . If not specified, and the amount parameter is a number, it will be interpreted as a number of milliseconds. The shorthand keys are: 'y', 'Q', 'M', 'w', 'd', 'h', 'm', 's' and 'ms' for years, quarter, months, weeks, days, hours, minutes, seconds and milliseconds respectively. |
Examples:
Format | Output |
---|---|
{{ 3 | duration("hours") | duration("humanize") }} | 3 hours |
{{ 2 | duration("hours") | duration("locale", "fr") | duration("humanize") }} | 2 heures |
{{ 3 | duration("hours") | duration("humanize", true) }} | in 3 hours |
List of tokens:
Token | Output | Example | ||
---|---|---|---|---|
Token | Output | |||
Month | M | 1 2 3 .... 11 12 | {{ now | date ('M') }} | October |
Mo | 1st 2nd 3rd .... 11th 12th | {{ now | date ('Mo') }} | 10th | |
MM | 01 02 03 .... 11 12 | {{ now | date ('MM') }} | 10 | |
MMM | Jan Feb Mar ... Nov Dec | {{ now | date ('MMM') }} | Oct | |
MMMM | January February ... December | {{ now | date ('MMMM') }} | October | |
Quarter | Q | 1 2 3 4 | {{ now | date ('Q') }} | 4 |
Qo | 1st 2nd 3rd 4th | {{ now | date ('Qo') }} | 4th | |
Day of Month | D | 1 2 3 4 5 .... 28 29 30 31 | {{ now | date ('D') }} | 14 |
Do | 1st 2nd 3rd... 30th 31st | {{ now | date ('Do') }} | 14th | |
DD | 01 02 03 .... 30 31 | {{ now | date ('DD') }} | 14 | |
Day of Year | DDD | 1 2 ... 364 365 | {{ now | date ('DDD') }} | 288 |
DDDo | 1st 2nd 3rd .... 364th 365th | {{ now | date ('DDDo') }} | 288th | |
DDDD | 001 002 003 ... 364 365 | {{ now | date ('DDDD') }} | 288 | |
Day of Week | d | 0 1 2 3 4 5 6 | {{ now | date ('d') }} | 5 |
do | 0th 1st 2nd 3rd 4th 5th 6th | {{ now | date ('do') }} | 5th | |
dd | Su Mo Tu We Th Fr Sa | {{ now | date ('dd') }} | Fr | |
ddd | Sun Mon Tue Wed Thu Fri Sat | {{ now | date ('ddd') }} | Fri | |
dddd | Sunday Monday ... Saturday | {{ now | date ('dddd') }} | Friday | |
Day of Week (Locale) | e | 0 1 2 3 4 5 6 | {{ now | date ('e') }} | 5 |
Day of Week (ISO) | E | 1 2 3 4 5 6 7 | {{ now | date ('E') }} | 5 |
Week of Year | w | 1 2 3 .. 52 53 | {{ now | date ('w') }} | 42 |
wo | 1st 2nd .. 52nd 53rd | {{ now | date ('wo') }} | 42nd | |
ww | 01 02 03 ... 52 53 | {{ now | date ('ww') }} | 42 | |
Week of Year (ISO) | W | 1 2 3 .. 52 53 | {{ now | date ('W') }} | 42 |
Wo | 1st 2nd .. 52nd 53rd | {{ now | date ('Wo') }} | 42nd | |
WW | 01 02 03 ... 52 53 | {{ now | date ('WW') }} | 42 | |
Year | YY | 70 71 72 ... 29 30 | {{ now | date ('YY') }} | 16 |
YYYY | 1970 1971 ... 2029 2030 | {{ now | date ('YYYY') }} | 2016 | |
Y | 1970 1971 ... 9999 +10000 +10001 Note: This complies with the ISO 8601 standard for dates past the year 9999 | {{ now | date ('Y') }} | 2016 | |
Week Year | gg | 70 71 ... 29 30 | {{ now | date ('gg') }} | 16 |
gggg | 1970 1971 ... 2029 2030 | {{ now | date ('gggg') }} | 2016 | |
Week Year (ISO) | GG | 70 71 ... 29 30 | {{ now | date ('GG') }} | 16 |
GGGG | 1970 1971 ... 2029 2030 | {{ now | date ('GGGG') }} | 2016 | |
AM/PM | A | AM PM | {{ now | date ('A') }} | AM |
a | am pm | {{ now | date ('a') }} | am | |
Hour | H | 0 1 2 .. 23 24 | {{ now | date ('H') }} | 7 |
HH | 00 01 02 03 ... 23 24 | {{ now | date ('HH') }} | 07 | |
h | 1 2 3 ... 11 12 | {{ now | date ('h') }} | 7 | |
hh | 01 02 03 .. 11 12 | {{ now | date ('hh') }} | 07 | |
k | 1 2 3 ... 23 24 | {{ now | date ('k') }} | 7 | |
kk | 01 02 03 .. 23 24 | {{ now | date ('kk') }} | 07 | |
Minute | m | 0 1 2 .. 58 59 | {{ now | date ('m') }} | 26 |
mm | 00 01 02 .. 58 59 | {{ now | date ('mm') }} | 26 | |
Second | s | 0 1 2 .. 58 59 | {{ now | date ('s') }} | 27 |
ss | 00 01 02 .. 58 59 | {{ now | date ('ss') }} | 27 | |
Fractional Second | S | 0 1 2 .. 8 9 | {{ now | date ('S') }} | 6 |
SS | 00 01 ... 98 99 | {{ now | date ('SS') }} | 67 | |
SSS | 000 001 ... 998 999 | {{ now | date ('SSS') }} | 672 | |
SSSS ... SSSSSSSSS | 000[0..] 001[0..] ... 998[0..] 999[0..] | {{ now | date ('SSSS ... SSSSSSSSS') }} | 6720 ... 672000000 | |
Time zone | z or zz | EST CST ... MST PST |
|
|
Z | -07:00 -06:00 ... +06:00 | {{ now | date ('Z') }} | +00:00 | |
ZZ | -0700 -0600 ... +0600 | {{ now | date ('ZZ') }} | +0000 | |
Unix Timestamp | X | 1360013296 | {{ now | date ('X') }} | 1476436377 |
Unix Millisecond Timestamp | x | 1360013296123 | {{ now | date ('x') }} | 1476436377284 |
If you would like to add strings in format strings, you need to wrap the characters in square brackets. For example: {{ now | date ('[The issue was resolved on] Do MMMM YYYY dddd') }}
will display, The issue was resolved on 14th October 2016 Friday.
tz
Description:
You can parse and display dates in any timezone. By default this method will print the date in the timezone of the current user (the user running the transition). See here for all the available functions.
The list of possible time zones is available here.
Syntax:
{{ DATE | date('tz') }}
Examples:
Format | Output |
---|---|
{{ now | date('tz') }} | 2017-07-21T06:26:46+02:00 |
{{ now | date('tz',"Asia/Kolkata") | date('dddd, MMMM Do YYYY, h:mm:ss a') }} | Fri, October 14th 2016, 7:26:27 am |
{{ issue.fields.duedate | date('add',10,"days") | date('tz',"America/Los_Angeles") | date ('YYYY MMMM DD') }} | 2017 June 05 |
fromNow
Description:
fromNow() method can be used to display the time from now.
Syntax:
{{ DATE | date('fromNow') }}
Examples: If Due date is 24/Nov/16
and Created is 13/10/16
Input | Output |
---|---|
{{ issue.fields.duedate | date('fromNow') }} | in a month |
{{ issue.fields.created | date('fromNow') }} | a day ago |
You can remove the suffix 'ago' by passing true to the method. For example: {{ issue.fields.duedate | date('fromNow' , true) }}
will display: a month
The string displayed for each length of time is listed below:
Range | Key | Sample Output |
---|---|---|
0 to 45 seconds | s | a few seconds ago |
45 to 90 seconds | m | a minute ago |
90 seconds to 45 minutes | mm | 2 minutes ago ... 45 minutes ago |
45 to 90 minutes | h | an hour ago |
90 minutes to 22 hours | hh | 2 hours ago ... 22 hours ago |
22 to 36 hours | d | a day ago |
36 hours to 25 days | dd | 2 days ago ... 25 days ago |
25 to 45 days | M | a month ago |
45 to 345 days | MM | 2 months ago ... 11 months ago |
345 to 545 days (1.5 years) | y | a year ago |
546 days+ | yy | 2 years ago ... 20 years ago |
from x
Description:
from() method can be used to display the time from x (time other than now).
Syntax:
{{ DATE | date('from' , 'x') }}
Examples:
If you want to find the time from the Due date to the Created date:
Input | Output |
---|---|
{% set due = issue.fields.duedate %} {% set cre = issue.fields.created %} {{ due | date('from' , cre) }} | in a month |
You can remove the suffix 'month' by passing true to the method: {{ due | date('from' , cre , true) }}
will display: a month.
toNow
Description:
toNow() method can be used to display the time to now.
Syntax:
{{ DATE | date('toNow') }}
Examples:
If the Due date is 24/Nov/16
, {{ issue.fields.duedate | date('fromNow') }}
will display: a month ago
If the issue created date is 13/10/16
, {{ issue.fields.created | date('fromNow') }}
will display: in a day
The string displayed for each length of time is listed below:
Range | Key | Sample Output |
---|---|---|
0 to 45 seconds | s | in seconds |
45 to 90 seconds | m | in a minute |
90 seconds to 45 minutes | mm | in 2 minutes ... in 45 minutes |
45 to 90 minutes | h | in an hour |
90 minutes to 22 hours | hh | in 2 hours ... in 22 hours |
22 to 36 hours | d | in a day |
36 hours to 25 days | dd | in 2 days ... in 25 days |
25 to 45 days | M | in a month |
45 to 345 days | MM | in 2 months ... in 11 months |
345 to 547 days (1.5 years) | y | in a year |
548 days+ | yy | in 2 years ... in 20 years |
to x
Description:
to() method can be used to display the time to x (time other than now).
Syntax:
{{ DATE | date('to' , 'x') }}
Examples:
If you want to find the time to the due date to the created date:
{% set due = issue.fields.duedate %} {% set cre = issue.fields.created %} {{ cre | date('to' , due) }}
This will display, in a month.
The string displayed for each length of time is listed below:
Range | Key | Sample Output |
---|---|---|
0 to 45 seconds | s | in seconds |
45 to 90 seconds | m | in a minute |
90 seconds to 45 minutes | mm | in 2 minutes ... in 45 minutes |
45 to 90 minutes | h | in an hour |
90 minutes to 22 hours | hh | in 2 hours ... in 22 hours |
22 to 36 hours | d | in a day |
36 hours to 25 days | dd | in 2 days ... in 25 days |
25 to 45 days | M | in a month |
45 to 345 days | MM | in 2 months ... in 11 months |
345 to 547 days (1.5 years) | y | in a year |
548 days+ | yy | in 2 years ... in 20 years |
You can remove the suffix 'in' by passing true
to the method: {{ cre | date('to' , due) }}
will display: a month.