The format()
function formats its argument as a string. It accepts all data types, including dates, Booleans, numbers, and even strings.
format(number)
format(Boolean)
format(date)
format(string)
format(list)
number.format()
Boolean.format()
date.format()
string.format()
list.format()
Code language: JavaScript (javascript)
format()
is very useful for converting data types within Notion formulas.
Good to know: Notion formulas can only output a single data type, and comparison operators can only compare data that share the same type.
See Converting Data Types to learn all of the ways to convert data in Notion formulas.
Example Formulas
format(4) /* Output: "4" (as a string) */
format(5+5) /* Output: "10" (as a string) */
format(true) /* Output: "true" (as a string) */
format(5>4) /* Output: "true" (as a string) */
format(now()) /* Output: "June 20, 2022 2:23 PM" (as a string, changes with now()'s value) */
/* Automatic conversion of a number to a string */
"There are " + prop("Number") + " Straw Hat members."
/* Output: There are 10 Straw Hat members. */
Code language: JavaScript (javascript)
Example Database
This example database uses the format()
function to output a string stating the age of each character.
View and Duplicate Database
“Age” Property Formula
"⏳ "
+ prop("Name")
+ " is "
+ dateBetween(
now(),
prop("Birthday"),
"years"
)
+ " years old."
Code language: JavaScript (javascript)
This formula outputs a sentence stating how old each character is based on their birthday.
To obtain the character’s age, we use the dateBetween function to find the number of years between now and the character’s birthday.
dateBetween()
returns a number, so we use the format()
function to convert it to a string.
Note: To keep thinks simple, this example formula doesn’t use conditional logic to determine whether the string should output “year” or “years” (accounting for plurality). To see an example that does, check out the length function.