Skip to main content

Functions

This page provides details of all available functions.

Functions by Type

Data Access Functions

firstStr, firstVal, lastStr, lastVal, submissionStr, submissionVal

Date Functions

new Date, dateAdd, dateFormat, dateFormatDistance, dateSubtract, differenceInDays, differenceInHours, differenceInMinutes, now

Math Functions

min, max, power

Function Reference

Date

The Date function constructs a date object from parts like year, month day, etc:

The Date function must be used with the new keyword (to maintain compatibility with JavaScript).

Syntax

new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)

Given at least a year and month, this form of Date() returns a Date object whose component values (year, month, day, hour, minute, second, and millisecond) all come from the following parameters. Any missing fields are given the lowest possible value (1 for day and 0 for every other component). The parameter values are all evaluated against the local time zone, rather than UTC.

If any parameter overflows its defined bounds, it "carries over". For example, if a monthIndex greater than 11 is passed in, those months will cause the year to increment; if a minutes greater than 59 is passed in, hours will increment accordingly, etc. Therefore, new Date(1990, 12, 1) will return January 1st, 1991; new Date(2020, 5, 19, 25, 65) will return 2:05 A.M. June 20th, 2020.

Similarly, if any parameter underflows, it "borrows" from the higher positions. For example, new Date(2020, 5, 0) will return May 31st, 2020.

  • year: Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year. See the example.
  • monthIndex: Integer value representing the month, beginning with 0 for January to 11 for December.
  • day: Optional Integer value representing the day of the month. The default is 1.
  • hours: Optional Integer value between 0 and 23 representing the hour of the day. Defaults to 0.
  • minutes: Optional Integer value representing the minute segment of a time. The default is 0 minutes past the hour.
  • seconds: Optional Integer value representing the second segment of a time. The default is 0 seconds past the minute.
  • milliseconds: Optional Integer value representing the millisecond segment of a time. The default is 0 milliseconds past the second.

Examples

new Date(1999, 9)
// Fri Oct 01 1999 00:00:00 GMT-0400 (Eastern Daylight Time)

new Date(1999, 9, 9)
// Sat Oct 09 1999 00:00:00 GMT-0400 (Eastern Daylight Time)

new Date(1999, 9, 9, 9)
// Sat Oct 09 1999 09:00:00 GMT-0400 (Eastern Daylight Time)

new Date(1999, 9, 9, 9, 9)
// Sat Oct 09 1999 09:09:00 GMT-0400 (Eastern Daylight Time)

new Date(1999, 9, 9, 9, 9, 9)
// Sat Oct 09 1999 09:09:09 GMT-0400 (Eastern Daylight Time)

new Date(1999, 9, 9, 9, 9, 9, 9)
// Sat Oct 09 1999 09:09:09 GMT-0400 (Eastern Daylight Time)

dateAdd

Add the specified years, months, weeks, days, hours, minutes and seconds to the given date.

Syntax


dateAdd(date, duration)

date - Date the date to be changed

duration - the object with years, months, weeks, days, hours, minutes and seconds to be added. Positive decimals will be rounded using Math.floor, decimals less than zero will be rounded using Math.ceil.

  • years Amount of years to be added
  • months Amount of months to be added
  • weeks Amount of weeks to be added
  • days Amount of days to be added
  • hours Amount of hours to be added
  • minutes Amount of minutes to be added
  • seconds Amount of seconds to be added

All values default to 0

Example

// Add the following duration to 1 September 2014, 10:19:50
dateAdd(new Date(2014, 8, 1, 10, 19, 50), {
years: 2,
months: 9,
weeks: 1,
days: 7,
hours: 5,
minutes: 9,
seconds: 30,
})
//=> Thu Jun 15 2017 15:29:20

Sourced from https://date-fns.org/v2.29.3/docs/add

dateFormat

Return the formatted date string in the given format. The result may vary by locale.

caution

Please note that the format tokens differ from Moment.js and other libraries. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

The characters wrapped between two single quotes characters (') are escaped. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote. (see the last example)

Format of the string is based on Unicode Technical Standard #35: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table with a few additions (see note 7 below the table).

UnitPatternResult examplesNotes
EraG..GGGAD, BC
GGGGAnno Domini, Before Christ2
GGGGGA, B
Calendar yeary44, 1, 1900, 20175
yo44th, 1st, 0th, 17th5,7
yy44, 01, 00, 175
yyy044, 001, 1900, 20175
yyyy0044, 0001, 1900, 20175
yyyyy...3,5
Local week-numbering yearY44, 1, 1900, 20175
Yo44th, 1st, 1900th, 2017th5,7
YY44, 01, 00, 175,8
YYY044, 001, 1900, 20175
YYYY0044, 0001, 1900, 20175,8
YYYYY...3,5
ISO week-numbering yearR-43, 0, 1, 1900, 20175,7
RR-43, 00, 01, 1900, 20175,7
RRR-043, 000, 001, 1900, 20175,7
RRRR-0043, 0000, 0001, 1900, 20175,7
RRRRR...3,5,7
Extended yearu-43, 0, 1, 1900, 20175
uu-43, 01, 1900, 20175
uuu-043, 001, 1900, 20175
uuuu-0043, 0001, 1900, 20175
uuuuu...3,5
Quarter (formatting)Q1, 2, 3, 4
Qo1st, 2nd, 3rd, 4th7
QQ01, 02, 03, 04
QQQQ1, Q2, Q3, Q4
QQQQ1st quarter, 2nd quarter, ...2
QQQQQ1, 2, 3, 44
Quarter (stand-alone)q1, 2, 3, 4
qo1st, 2nd, 3rd, 4th7
qq01, 02, 03, 04
qqqQ1, Q2, Q3, Q4
qqqq1st quarter, 2nd quarter, ...2
qqqqq1, 2, 3, 44
Month (formatting)M1, 2, ..., 12
Mo1st, 2nd, ..., 12th7
MM01, 02, ..., 12
MMMJan, Feb, ..., Dec
MMMMJanuary, February, ..., December2
MMMMMJ, F, ..., D
Month (stand-alone)L1, 2, ..., 12
Lo1st, 2nd, ..., 12th7
LL01, 02, ..., 12
LLLJan, Feb, ..., Dec
LLLLJanuary, February, ..., December2
LLLLLJ, F, ..., D
Local week of yearw1, 2, ..., 53
wo1st, 2nd, ..., 53th7
ww01, 02, ..., 53
ISO week of yearI1, 2, ..., 537
Io1st, 2nd, ..., 53th7
II01, 02, ..., 537
Day of monthd1, 2, ..., 31
do1st, 2nd, ..., 31st7
dd01, 02, ..., 31
Day of yearD1, 2, ..., 365, 3669
Do1st, 2nd, ..., 365th, 366th7
DD01, 02, ..., 365, 3669
DDD001, 002, ..., 365, 366
DDDD...3
Day of week (formatting)E..EEEMon, Tue, Wed, ..., Sun
EEEEMonday, Tuesday, ..., Sunday2
EEEEEM, T, W, T, F, S, S
EEEEEEMo, Tu, We, Th, Fr, Sa, Su
ISO day of week (formatting)i1, 2, 3, ..., 77
io1st, 2nd, ..., 7th7
ii01, 02, ..., 077
iiiMon, Tue, Wed, ..., Sun7
iiiiMonday, Tuesday, ..., Sunday2,7
iiiiiM, T, W, T, F, S, S7
iiiiiiMo, Tu, We, Th, Fr, Sa, Su7
Local day of week (formatting)e2, 3, 4, ..., 1
eo2nd, 3rd, ..., 1st7
ee02, 03, ..., 01
eeeMon, Tue, Wed, ..., Sun
eeeeMonday, Tuesday, ..., Sunday2
eeeeeM, T, W, T, F, S, S
eeeeeeMo, Tu, We, Th, Fr, Sa, Su
Local day of week (stand-alone)c2, 3, 4, ..., 1
co2nd, 3rd, ..., 1st7
cc02, 03, ..., 01
cccMon, Tue, Wed, ..., Sun
ccccMonday, Tuesday, ..., Sunday2
cccccM, T, W, T, F, S, S
ccccccMo, Tu, We, Th, Fr, Sa, Su
AM, PMa..aaAM, PM
aaaam, pm
aaaaa.m., p.m.2
aaaaaa, p
AM, PM, noon, midnightb..bbAM, PM, noon, midnight
bbbam, pm, noon, midnight
bbbba.m., p.m., noon, midnight2
bbbbba, p, n, mi
Flexible day periodB..BBBat night, in the morning, ...
BBBBat night, in the morning, ...2
BBBBBat night, in the morning, ...
Hour [1-12]h1, 2, ..., 11, 12
ho1st, 2nd, ..., 11th, 12th7
hh01, 02, ..., 11, 12
Hour [0-23]H0, 1, 2, ..., 23
Ho0th, 1st, 2nd, ..., 23rd7
HH00, 01, 02, ..., 23
Hour [0-11]K1, 2, ..., 11, 0
Ko1st, 2nd, ..., 11th, 0th7
KK01, 02, ..., 11, 00
Hour [1-24]k24, 1, 2, ..., 23
ko24th, 1st, 2nd, ..., 23rd7
kk24, 01, 02, ..., 23
Minutem0, 1, ..., 59
mo0th, 1st, ..., 59th7
mm00, 01, ..., 59
Seconds0, 1, ..., 59
so0th, 1st, ..., 59th7
ss00, 01, ..., 59
Fraction of secondS0, 1, ..., 9
SS00, 01, ..., 99
SSS000, 001, ..., 999
SSSS...3
Timezone (ISO-8601 w/ Z)X-08, +0530, Z
XX-0800, +0530, Z
XXX-08:00, +05:30, Z
XXXX-0800, +0530, Z, +1234562
XXXXX-08:00, +05:30, Z, +12:34:56
Timezone (ISO-8601 w/o Z)x-08, +0530, +00
xx-0800, +0530, +0000
xxx-08:00, +05:30, +00:002
xxxx-0800, +0530, +0000, +123456
xxxxx-08:00, +05:30, +00:00, +12:34:56
Timezone (GMT)O...OOOGMT-8, GMT+5:30, GMT+0
OOOOGMT-08:00, GMT+05:30, GMT+00:002
Timezone (specific non-locat.)z...zzzGMT-8, GMT+5:30, GMT+06
zzzzGMT-08:00, GMT+05:30, GMT+00:002,6
Seconds timestampt5129695207
tt...3,7
Milliseconds timestampT5129695209007
TT...3,7
Long localized dateP04/29/14537
PPApr 29, 14537
PPPApril 29th, 14537
PPPPFriday, April 29th, 14532,7
Long localized timep12:00 AM7
pp12:00:00 AM7
ppp12:00:00 AM GMT+27
pppp12:00:00 AM GMT+02:002,7
Combination of date and timePp04/29/1453, 12:00 AM7
PPppApr 29, 1453, 12:00:00 AM7
PPPpppApril 29th, 1453 at ...7
PPPPppppFriday, April 29th, 1453 at ...2,7

Notes:

  1. "Formatting" units (e.g. formatting quarter) in the default en-US locale are the same as "stand-alone" units, but are different in some languages. "Formatting" units are declined according to the rules of the language in the context of a date. "Stand-alone" units are always nominative singular:
format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'

format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'
  1. Any sequence of the identical letters is a pattern, unless it is escaped by the single quote characters (see below). If the sequence is longer than listed in table (e.g. EEEEEEEEEEE) the output will be the same as default pattern for this unit, usually the longest one (in case of ISO weekdays, EEEE). Default patterns for units are marked with "2" in the last column of the table.
format(new Date(2017, 10, 6), 'MMM') //=> 'Nov'

format(new Date(2017, 10, 6), 'MMMM') //=> 'November'

format(new Date(2017, 10, 6), 'MMMMM') //=> 'N'

format(new Date(2017, 10, 6), 'MMMMMM') //=> 'November'

format(new Date(2017, 10, 6), 'MMMMMMM') //=> 'November'
  1. Some patterns could be unlimited length (such as yyyyyyyy). The output will be padded with zeros to match the length of the pattern.
format(new Date(2017, 10, 6), 'yyyyyyyy') //=> '00002017'
  1. QQQQQ and qqqqq could be not strictly numerical in some locales. These tokens represent the shortest form of the quarter.

  2. The main difference between y and u patterns are B.C. years:

Yearyu
AC 111
BC 110
BC 22-1

Also yy always returns the last two digits of a year, while uu pads single digit years to 2 characters and returns other years unchanged:

Yearyyuu
10101
141414
37676376
1453531453

The same difference is true for local and ISO week-numbering years (Y and R), except local week-numbering years are dependent on options.weekStartsOn and options.firstWeekContainsDate (compare getISOWeekYear and getWeekYear).

  1. Specific non-location timezones are currently unavailable in date-fns, so right now these tokens fall back to GMT timezones.

  2. These patterns are not in the Unicode Technical Standard #35:

  • i: ISO day of week
  • I: ISO week of year
  • R: ISO week-numbering year
  • t: seconds timestamp
  • T: milliseconds timestamp
  • o: ordinal number modifier
  • P: long localized date
  • p: long localized time
  1. YY and YYYY tokens represent week-numbering years but they are often confused with years. You should enable options.useAdditionalWeekYearTokens to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

  2. D and DD tokens represent days of the year but they are often confused with days of the month. You should enable options.useAdditionalDayOfYearTokens to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

Syntax

formatDate(date, format)
  • date - Date the original date
  • format - the string of tokens

Example

// Represent 11 February 2014 in middle-endian format:
const result = formatDate(new Date(2014, 1, 11), 'MM/dd/yyyy')
//=> '02/11/2014'

Sourced from https://date-fns.org/v2.29.3/docs/format

dateFormatDistance

Return the distance between the given dates in words.

Distance between datesResult
0 ... 30 secsless than a minute
30 secs ... 1 min 30 secs1 minute
1 min 30 secs ... 44 mins 30 secs[2..44] minutes
44 mins ... 30 secs ... 89 mins 30 secsabout 1 hour
89 mins 30 secs ... 23 hrs 59 mins 30 secsabout [2..24] hours
23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs1 day
41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs[2..30] days
29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secsabout 1 month
44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secsabout 2 months
59 days 23 hrs 59 mins 30 secs ... 1 yr[2..12] months
1 yr ... 1 yr 3 monthsabout 1 year
1 yr 3 months ... 1 yr 9 month sover 1 year
1 yr 9 months ... 2 yrsalmost 2 years
N yrs ... N yrs 3 monthsabout N years
N yrs 3 months ... N yrs 9 monthsover N years
N yrs 9 months ... N+1 yrsalmost N+1 years

Syntax

formatDistance(date, baseDate, [options])
  • date - Date, the date
  • baseDate - Date, the date to compare with
  • options - (optional) missing or {addSuffix: true} to indicate to add "ago" or {includeSeconds: true} to include seconds (or can be combined like {addSuffix: true, includeSeconds: true} )

Example

// What is the distance between 2 July 2014 and 1 January 2015?
dateFormatDistance(new Date(2014, 6, 2), new Date(2015, 0, 1))
//=> '6 months'


// What is the distance between 1 January 2015 00:00:15
// and 1 January 2015 00:00:00, including seconds?
dateFormatDistance(
new Date(2015, 0, 1, 0, 0, 15),
new Date(2015, 0, 1, 0, 0, 0),
{ includeSeconds: true }
)
//=> 'less than 20 seconds'

// What is the distance from 1 January 2016
// to 1 January 2015, with a suffix?
dateFormatDistance(new Date(2015, 0, 1), new Date(2016, 0, 1), {
addSuffix: true
})
//=> 'about 1 year ago'


Sourced from https://date-fns.org/v2.29.3/docs/formatDistance

dateSubtract

Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.

Syntax

dateSubtract(date, duration)
  • date - Date, the date to be changed
  • duration - the object with years, months, weeks, days, hours, minutes and seconds to be subtracted
KeyDescription
yearsAmount of years to be subtracted
monthsAmount of months to be subtracted
weeksAmount of weeks to be subtracted
daysAmount of days to be subtracted
hoursAmount of hours to be subtracted
minutesAmount of minutes to be subtracted
secondsAmount of seconds to be subtracted

Example

// Subtract the following duration from 15 June 2017 15:29:20
dateSubtract(new Date(2017, 5, 15, 15, 29, 20), {
years: 2,
months: 9,
weeks: 1,
days: 7,
hours: 5,
minutes: 9,
seconds: 30
})
//=> Mon Sep 1 2014 10:19:50

Sourced from https://date-fns.org/v2.29.3/docs/formatDistance

differenceInDays

Get the number of full day periods between two dates. Fractional days are truncated towards zero.

One "full day" is the distance between a local time in one day to the same local time on the next or previous day. A full day can sometimes be less than or more than 24 hours if a daylight savings change happens between two dates.

Syntax

differenceInDays(dateLeft, dateRight)
  • dateLeft - Date, the later date
  • dateRight - Date, the earlier date

Examples

// How many full days are between
// 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
differenceInDays(
new Date(2012, 6, 2, 0, 0),
new Date(2011, 6, 2, 23, 0)
)
//=> 365

// How many full days are between
// 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
differenceInDays(
new Date(2011, 6, 3, 0, 1),
new Date(2011, 6, 2, 23, 59)
)
//=> 0

// How many full days are between
// 1 March 2020 0:00 and 1 June 2020 0:00 ?
// Note: because local time is used, the
// result will always be 92 days, even in
// time zones where DST starts and the
// period has only 92*24-1 hours.
const result = differenceInDays(
new Date(2020, 5, 1),
new Date(2020, 2, 1)
)
//=> 92

Sourced from https://date-fns.org/v2.29.3/docs/differenceInDays

differenceInHours

Get the number of hours between the given dates

Syntax


differenceInHours(dateLeft, dateRight)

  • dateLeft - Date, the later date
  • dateRight - Date, the earlier date

Example


// How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
differenceInHours(
new Date(2014, 6, 2, 19, 0),
new Date(2014, 6, 2, 6, 50)
)
//=> 12

Sourced from https://date-fns.org/v2.29.3/docs/differenceInHours

differenceInMinutes

Get the signed number of full (rounded towards 0) minutes between the given dates

Syntax

differenceInMinutes(dateLeft, dateRight)

  • dateLeft - Date, the later date
  • dateRight - Date, the earlier date

Example

// How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
differenceInMinutes(
new Date(2014, 6, 2, 12, 20, 0),
new Date(2014, 6, 2, 12, 7, 59)
)
//=> 12

// How many minutes are between 10:01:59 and 10:00:00
differenceInMinutes(
new Date(2000, 0, 1, 10, 0, 0),
new Date(2000, 0, 1, 10, 1, 59)
)
//=> -1

max

Determine the maximum of a list of numbers.

Syntax

max(number1, number2, number3, ...)

Example

max(1,2,3)
// => 3

min

Determine the minimum of a list of numbers.

Syntax

min(number1, number2, number3, ...)

Example

min(1,2,3)
// => 1

now

Returns the current date. It is sensitive to the Simulator's as at feature so sometimes it will return the simulated date, not the actual date.

Syntax

now()

Example

now()
// => current date time

firstStr

firstVal

firstVal and firstStr retrieve the first submitted value of the specific data reference.

Note for the carePlan entity, there is no difference between firstVal and lastVal since there is only one instance of a plan.

If the form was never submitted it returns undefined

Str vs Val versions

The difference between the Str and Val variations is the data type that is returned. If Str is used, the value is ready to be displayed to a user and the system will attempt to format it with whatever metadata can be found. In the future, this will pick up units labels and decimals defined by a Unit of Measure. So use Str versions when you want to display the value in a view.

The Val version should be used if you wanted to interrogate the value. For example if you wanted to use min, max, > against the results.

Syntax

firstStr("data reference")
firstVal("data reference")

Examples

firstStr("submission.form1.field1")
// Returns a printable string of field1 in form1 the first time it was submitted

firstVal("submission.form1.field1")
// Returns the value of field1 in form1 the first time it was submitted

firstStr("carePlan.status")
// Returns the plan status

lastStr

lastVal

lastVal and lastStr retrieve the latest submitted value of the specific data reference.

Note for the carePlan entity, there is no difference between firstVal and lastVal since there is only one instance of a plan.

If the form was never submitted it returns undefined

See Str vs Val versions

Syntax

lastStr("data reference")
lastVal("data reference")

Examples

lastStr("submission.form1.field1")
// Returns a printable string of field1 in form1 the last time it was submitted

lastVal("submission.form1.field1")
// Returns the value of field1 in form1 the last time it was submitted

lastStr("carePlan.status")
// Returns the plan status

submissionStr

submissionVal

submissionVal and submissionStr retrieve the submitted value of the specific data reference for the form submission that is in context.

See Str vs Val versions

Unlike lastVal, etc., use only the field name. "submission.form-name" is implied. You may not access plan attributes.

Syntax

submissionStr("field name")
submissionVal("field name")

Examples

submissionStr("field1")
// Returns a printable string of field1 in the form that was submitted

submissionVal("field1")
// Returns the value of field1 in the form that was submitted

power

The power function computes an exponential.

Syntax

power(base,exp)

Example

power(2,2)
// => 4