
Function: The Format Function
The FORMAT function is used to create strings with mixed content. It takes a format string and a list of parameters to fill in the format specifiers. This function is similar to the C and C++ "sprintf" function and uses the same format specifiers. You can refer to the reference for sprintf for detailed documentation about the format specifiers.
Prototype:
<string> = format(<format string>, var1, var2, ...var-n);
Parameters:
Parameter Name | Type | Description |
---|---|---|
format string | string | A string literal or variable containing the format string. |
mixed list | any | A list of variables or constants separated by commas. The number of parameters must match the number of format specifiers in the format string. |
Return Value: Type String: A formatted string according to the format specifiers and the variables.
Commonly Used Format Specifiers:
Format Specifier | Data Type | Options and Description |
---|---|---|
%s | string | The length of the string can be specified and padded using a number between the % and s . |
%d | int | The length of the string can be specified and padded using a number between the % and d . %10d means space-padded right-justified field of ten positions. %010d means the same but zero-padded. %-10d means space-padded left-justified string of 10 positions. |
%ld | long | Similar to %d , but for long integers. |
%f | float or double | %10.2f means a float with ten positions and two decimal places, right-justified. %10f means no specified length but with two decimal places. |
Note: For variables of the types of date, time, and timestamp needing several format codes for each variable, the best option is to use the %s
format specifier, and then use the string function to format the variable individually, using the spec found under that heading below.
Typical Use Cases:
The FORMAT function is commonly used for constructing return values, building SQL statements, creating FileMaker calculations, generating messages, and forming strings for data exports.
Example:
string s1 = "one", s2 = "two";
print format("This is a fixed string with three other strings in it 1:%s, 2:%s, 3:%s", s1, s2, "last is a constant string");
// Output to the console:
// This is a fixed string with three other strings in it 1:one, 2:two, 3:last is a constant string
// Format with dates in d.m.y format.
date today = now();
string formatted = format("Today is %s and tomorrow is %s", string(today, "%d.%m.%Y"), string(today + 1, "%d.%m.%Y"));
print formatted;
// Output to the console:
// Today is 12.11.2018 and tomorrow is 13.11.2018
// Converting SQL dates and using them in the format.
string sqlDate = "2018-11-12";
date myDate = date(sqlDate, "%Y-%m-%d");
print format("The SQL date is: %s, or in US format %s.", string(myDate, "%d.%m.%Y"), string(myDate, "%m/%d/%Y"));
// Output to the console:
// The SQL date is: 12.11.2018, or in US format 11/12/2018.
The FORMAT function allows you to create formatted strings with various data types and is useful for a wide range of applications, including working with dates, generating SQL statements, and formatting messages.
Formatting Date and Time values
For variables of type date, time, and timestamp, they can be formatted using the string function. The string function converts any data type to a string representation, and for date and time types you can have a second parameter: the format string. This adds flexibility to create strings in the desired format, like SQL format or any other format needed.
The following types of format specifiers can be used:
specifier | Replaced by | Example |
---|---|---|
%a | Abbreviated weekday name * | Thu |
%A | Full weekday name * | Thursday |
%b | Abbreviated month name * | Aug |
%B | Full month name * | August |
%c | Date and time representation * | Thu Aug 23 14:55:02 2001 |
%C | Year divided by 100 and truncated to integer (00-99) | 20 |
%d | Day of the month, zero-padded (01-31) | 23 |
%D | Short MM/DD/YY date, equivalent to %m/%d/%y | 08/23/01 |
%e | Day of the month, space-padded ( 1-31) | 23 |
%F | Short YYYY-MM-DD date, equivalent to %Y-%m-%d | 23.08.2001 |
%g | Week-based year, last two digits (00-99) | 1 |
%G | Week-based year | 2001 |
%h | Abbreviated month name * (same as %b) | Aug |
%H | Hour in 24h format (00-23) | 14 |
%I | Hour in 12h format (01-12) | 2 |
%j | Day of the year (001-366) | 235 |
%m | Month as a decimal number (01-12) | 8 |
%M | Minute (00-59) | 55 |
%n | New-line character ('\n') | |
%p | AM or PM designation | PM |
%r | 12-hour clock time * | 2:55:02 pm |
%R | 24-hour HH:MM time, equivalent to %H:%M | 14:55 |
%S | Second (00-61) | 2 |
%t | Horizontal-tab character ('\t') | |
%T | ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S | 14:55:02 |
%u | ISO 8601 weekday as number with Monday as 1 (1-7) | 4 |
%U | Week number with the first Sunday as the first day of week one (00-53) | 33 |
%V | ISO 8601 week number (01-53) | 34 |
%w | Weekday as a decimal number with Sunday as 0 (0-6) | 4 |
%W | Week number with the first Monday as the first day of week one (00-53) | 34 |
%x | Date representation * | 08/23/01 |
%X | Time representation * | 14:55:02 |
%y | Year, last two digits (00-99) | 1 |
%Y | Year | 2001 |
%z | ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100) ** | 100 |
%Z | Timezone name or abbreviation *, ** | CDT |
%% | A % sign | % |
* The specifiers marked with an asterisk (*) are locale-dependent.
** If the timezone cannot be determined, no characters
Using format strings for parsing date, time, and timestamp
When you have a date, time, or timestamp value, it can be parsed using the function's date
, time
, or timestamp
. Those functions convert a string into the appropriate format. Those functions have an optional second parameter: The format string. If the source string is a SQL formatted date, having "%Y-%m-%d"
as the second parameter to the date
function will parse the date correctly.