There will be many occasions when you will need to display information in a certain way or display multiple variable types on a single line. The FORMAT
function will help you change almost any data type into a string that can be manipulated in any way you see fit.
Create a new codeunit from Object Designer.
Add the following global variables:
Name
Type
Length
FormattedDate
Text
30
Add the following code to the
OnRun
trigger:FormattedDate := FORMAT(TODAY, 0, '<Month Text> <Day,2>, <Year4>'); MESSAGE('Today is %1', FormattedDate);
Save and close the codeunit.
When you run the codeunit you should see a window similar to the following :
The FORMAT
function takes one to three parameters. The first parameter is required and can be of almost any type: date, time, integer, decimal, and so on. This parameter is returned as a string.
The second parameter is the length of the string to be returned. A default zero means that the entire string will be returned. A positive number tells the function to return a string of exactly that length, and a negative number returns a string no larger than that length.
There are two options for the third and final parameter. One is a number, representing a predefined format you want to use for the string and the other is a literal string. In the example, we used the actual format string. The text contained in brackets (< >) will be parsed and replaced with the data in the first parameter.
There are many predefined formats for dates. The examples listed in the following table are taken from the C/SIDE Reference Guide in the Help menu of the NAV client. Search for "Format Property" to find more information.
Date |
Format |
Example |
---|---|---|
|
|
05-04-03 |
|
|
05-04-03 |
|
|
050403D |
|
|
03-04-05 |
|
|
5. April 2003 |
|
|
050403 |
|
|
030405 |
|
|
5. Apr 2003 |
|
|
2003-04-05 |
You will often need to create filters on dates or other simple data types. Usually these filters are not just for a single value. For example, a date filter for all values between January 1st, 2010 and January 31st, 2010 would look like 010110..013110
. Because ".." is a string, and you cannot concatenate it with two date variables. Instead, you will have to convert those dates into strings and then place the filters together.
Take the CreateAccountingDateFilter
function from codeunit 358, DateFilter-Calc. It creates date filters based on accounting periods for the exact scenario we are describing.
AccountingPeriod.RESET; IF FiscalYear THEN AccountingPeriod.SETRANGE("New Fiscal Year",TRUE); AccountingPeriod."Starting Date" := Date; AccountingPeriod.FIND('=<>'); IF AccountingPeriod."Starting Date" > Date THEN NextStep := NextStep - 1; IF NextStep <> 0 THEN IF AccountingPeriod.NEXT(NextStep) <> NextStep THEN BEGIN IF NextStep < 0 THEN Filter := '..' + FORMAT( AccountingPeriod."Starting Date" - 1) ELSE Filter := FORMAT(AccountingPeriod."Starting Date") + '..' + FORMAT(12319999D); Name := '...'; EXIT; END; StartDate := AccountingPeriod."Starting Date"; IF FiscalYear THEN Name := STRSUBSTNO(Text000,FORMAT(DATE2DMY(StartDate,3))) ELSE Name := AccountingPeriod.Name; IF AccountingPeriod.NEXT <> 0 THEN Filter := FORMAT(StartDate) + '..' + FORMAT(AccountingPeriod."Starting Date" - 1) ELSE BEGIN Filter := FORMAT(StartDate) + '..' + FORMAT(12319999D); Name := Name + '...'; END;