Our new Appfire Documentation Space is now live!

Take a look here! If you have any questions please email support@appfire.com

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

In the Nunjucks templates of JMWE, to obtain a durationObject from a duration number the difference between two moments is returned in milliseconds. For e.g. {{ nowObj | date("diff", issue.fields.created ) }} returns the duration from issue creation to now in milliseconds. To manipulate and format durations in Nunjucks, JMWE uses the duration objects of the Moment.js library. All the functions provided by the library, that are available to your Nunjucks templates through the duration filter, return a Moment.js duration object. A Moment.js duration object is an object that represents the length of time in milliseconds, and can easily be manipulated and formatted for display. Refer to the official documentation for more information about Moment.js.

On this page:

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.

{{ 3 | duration("minutes") }} outputs 180000

{{ "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: 

{{ 3 | duration("minutes") | duration("humanize") }} returns the current date/time as an ISO 8601-compliant string such as 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:

ParameterDescription
amountThe amount of time to add. Can be a number or another Moment.js duration object
unitThe unit of the amount parameter. One of secondsminuteshoursdaysweeksmonthsyears. 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:

InputDetailsOutput

{{ 3 | duration("days") | duration("add", 6, "days") }}

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 literal7

subtract

Description:

Subtracts time from a Momentjs duration.

Syntax:

{{ durationObject | duration('subtract' , amount [, unit ] ) }}

Parameters:

ParameterDescription
amountThe amount of time to subtract. Can be a number or another Moment.js duration object
unitThe unit of the amount parameter. One of secondsminuteshoursdaysweeksmonthsyears. 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:

InputDetailsOutput

{{ 3 | duration("days") | duration("subtract", 6, "days") }}

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 literal2016-06-04T08:51:47Z

get

Description:

Returns a part of the Moment.js duration (milliseconds, seconds, minutes, ...)

Syntax:

{{ durationObject | duration([unit]) }}

Parameters:

ParameterDescription
unitThe unit of the number to return. One of secondsminuteshoursdaysweeksmonthsyears. 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:

InputOutput

{{ 3 | duration("hours") | duration("get", "hours") }}

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:

ParameterDescription
unitThe unit of the number to return. One of secondsminuteshoursdaysweeksmonthsyears. 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:

InputOutput

{{ 3 | duration("hours") | duration("as", "minutes") }}

180
{{ "2:30:00" | duration | duration("as", "minutes") }}
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:

ParameterDescription
amountThe amount of time to subtract. Can be a number or another Moment.js duration object
unitParameterDescription

amountThe amount of time to subtract. Can be a number or another Moment.js duration object

unitThe unit of the amount parameter. One of secondsminuteshoursdaysweeksmonthsyears. 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:

FormatOutput
{{ 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
TokenOutput
MonthM1 2 3 .... 11 12{{ now | date ('M') }}October

Mo1st 2nd 3rd .... 11th 12th{{ now | date ('Mo') }}10th

MM01 02 03 .... 11 12{{ now | date ('MM') }}10

MMMJan Feb Mar ... Nov Dec{{ now | date ('MMM') }}Oct

MMMMJanuary February ... December{{ now | date ('MMMM') }}October
QuarterQ1 2 3 4{{ now | date ('Q') }}4

Qo1st 2nd 3rd 4th{{ now | date ('Qo') }}4th
Day of MonthD1 2 3 4 5 .... 28 29 30 31{{ now | date ('D') }}14

Do1st 2nd 3rd... 30th 31st{{ now | date ('Do') }}14th

DD01 02 03 .... 30 31{{ now | date ('DD') }}14
Day of YearDDD1 2 ... 364 365{{ now | date ('DDD') }}288

DDDo1st 2nd 3rd .... 364th 365th{{ now | date ('DDDo') }}288th

DDDD001 002 003 ... 364 365{{ now | date ('DDDD') }}288
Day of Weekd0 1 2 3 4 5 6{{ now | date ('d') }}5

do0th 1st 2nd 3rd 4th 5th 6th{{ now | date ('do') }}5th

ddSu Mo Tu We Th Fr Sa{{ now | date ('dd') }}Fr

dddSun Mon Tue Wed Thu Fri Sat{{ now | date ('ddd') }}Fri

ddddSunday Monday ... Saturday{{ now | date ('dddd') }}Friday
Day of Week (Locale)e0 1 2 3 4 5 6{{ now | date ('e') }}5
Day of Week (ISO)E1 2 3 4 5 6 7{{ now | date ('E') }}5
Week of Yearw1 2 3 .. 52 53{{ now | date ('w') }}42

wo1st 2nd .. 52nd 53rd{{ now | date ('wo') }}42nd

ww01 02 03 ... 52 53{{ now | date ('ww') }}42
Week of Year (ISO)W1 2 3 .. 52 53{{ now | date ('W') }}42

Wo1st 2nd .. 52nd 53rd{{ now | date ('Wo') }}42nd

WW01 02 03 ... 52 53{{ now | date ('WW') }}42
YearYY70 71 72 ... 29 30{{ now | date ('YY') }}16

YYYY1970 1971 ... 2029 2030{{ now | date ('YYYY') }}2016

Y1970 1971 ... 9999 +10000 +10001 
Note: This complies with the ISO 8601 standard for dates past the year 9999
{{ now | date ('Y') }}2016
Week Yeargg70 71 ... 29 30{{ now | date ('gg') }}16

gggg1970 1971 ... 2029 2030{{ now | date ('gggg') }}2016
Week Year (ISO)GG70 71 ... 29 30{{ now | date ('GG') }}16

GGGG1970 1971 ... 2029 2030{{ now | date ('GGGG') }}2016
AM/PMAAM PM{{ now | date ('A') }}AM

aam pm{{ now | date ('a') }}am
HourH0 1 2 .. 23 24{{ now | date ('H') }}7

HH00 01 02 03 ... 23 24{{ now | date ('HH') }}07

h1 2 3 ... 11 12{{ now | date ('h') }}7

hh01 02 03 .. 11 12{{ now | date ('hh') }}07

k1 2 3 ... 23 24{{ now | date ('k') }}7

kk01 02 03 .. 23 24{{ now | date ('kk') }}07
Minutem0 1 2 .. 58 59{{ now | date ('m') }}26

mm00 01 02 .. 58 59{{ now | date ('mm') }}26
Seconds0 1 2 .. 58 59{{ now | date ('s') }}27

ss00 01 02 .. 58 59{{ now | date ('ss') }}27
Fractional SecondS0 1 2 .. 8 9{{ now | date ('S') }}6

SS00 01 ... 98 99{{ now | date ('SS') }}67

SSS000 001 ... 998 999{{ now | date ('SSS') }}672

SSSS ... SSSSSSSSS000[0..] 001[0..] ... 998[0..] 999[0..]{{ now | date ('SSSS ... SSSSSSSSS') }}6720 ... 672000000
Time zonez or zzEST CST ... MST PST 

{{ now | date ('z') }}

{{ now | date ('zz') }}

UTC Coordinated Universal Time


Z

-07:00 -06:00 ... +06:00

{{ now | date ('Z') }}+00:00

ZZ-0700 -0600 ... +0600{{ now | date ('ZZ') }}+0000
Unix TimestampX1360013296{{ now | date ('X') }}1476436377
Unix Millisecond Timestampx1360013296123{{ 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:

FormatOutput
{{ 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

InputOutput
{{ 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: 

RangeKeySample Output
0 to 45 secondssa few seconds ago
45 to 90 secondsma minute ago
90 seconds to 45 minutesmm2 minutes ago ... 45 minutes ago
45 to 90 minuteshan hour ago
90 minutes to 22 hourshh2 hours ago ... 22 hours ago
22 to 36 hoursda day ago
36 hours to 25 daysdd2 days ago ... 25 days ago
25 to 45 daysMa month ago
45 to 345 daysMM2 months ago ... 11 months ago
345 to 545 days (1.5 years)ya year ago
546 days+yy2 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:

InputOutput
{% 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: 

RangeKeySample Output
0 to 45 secondssin seconds
45 to 90 secondsmin a minute
90 seconds to 45 minutesmmin 2 minutes ... in 45 minutes
45 to 90 minuteshin an hour
90 minutes to 22 hourshhin 2 hours ... in 22 hours
22 to 36 hoursdin a day
36 hours to 25 daysddin 2 days ... in 25 days
25 to 45 daysMin a month
45 to 345 daysMMin 2 months ... in 11 months
345 to 547 days (1.5 years)yin a year
548 days+yyin 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: 

RangeKeySample Output
0 to 45 secondssin seconds
45 to 90 secondsmin a minute
90 seconds to 45 minutesmmin 2 minutes ... in 45 minutes
45 to 90 minuteshin an hour
90 minutes to 22 hourshhin 2 hours ... in 22 hours
22 to 36 hoursdin a day
36 hours to 25 daysddin 2 days ... in 25 days
25 to 45 daysMin a month
45 to 345 daysMMin 2 months ... in 11 months
345 to 547 days (1.5 years)yin a year
548 days+yyin 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.

  • No labels