The manipulation of dates and times often requires specific formatting for various representations based on 4D’s internal resources or operating system settings.
While the default formats are usually all you need, there are scenarios in which custom formatting is essential, particularly for different geographical locations or unique requirements. In 4D v20 R4, significant advances have been made to meet this precise need
HDI: Tailored Customization for Dates and Times
What are the benefits?
As mentioned in the introduction, the number of formatting options is limited. If you wish to write a document for a destination other than the country in which it was created, the formats available will likely not be those desired by the recipient. It is, therefore, essential to define, down to the decimal point, how dates and times will be displayed in the end.
In addition, new possibilities are offered, such as week or quarter numbers or even the day of the year!
FORMATTING OR TRANSFORMATION?
There are two very different ways of formatting times and dates.
The first is to define a display mode in a form. In this case, a date remains a date. It can even be displayed several times, but in different ways, on a single form or document.
The second is to transform a date into a string using the String function.
In either case, you must define a formatting string to describe the desired result.
A common formatting chain
Whether displaying (simple formatting) or creating a string via the String function, the necessary parameter (also a string) will be common to both uses.
“EEEE dd/MM/yyyy”, “QQQQ” ou “HH:mm: ss”?
This is the most exciting part, opening up almost infinite possibilities. Not only can you decide on the order of the elements and how to present them, but other information that was previously inaccessible, at least in such a simple way, can be revealed.
The documentation details the definition and result obtained by each letter or combination of letters, but allow me to give you a few examples for dates…
15/06/2000 -> “QQQQ” -> “2nd quarter” (localised in your language like “2è trimestre” in french !)
17/03/2001 -> “D” -> “76” (76th day of the year, 77th in leap years)
17/03/1954 -> “w” -> “11” (11th week of the year – which varies according to the year!)
17/03/1954 -> “eeee” -> “Wednesday” ((You can double-check that it was a Wednesday 🙂 (Also localised)
A few examples with hours
15:00:00 -> “a” -> “PM”
18:00:00 -> “K” -> “6”
13:30:15 -> “hh:mm aa O” -> 01:30 PM GMT+1
Finally… the formatting string may also contain characters which, if between “a” and “z” or “A and “Z”, must be enclosed in single quotes to avoid being interpreted as formatting characters.
15:30:00 -> “HH ‘hours and’ mm ‘minutes'” -> “15 hours and 30 minutes”
The “String” COmmand
The String command has been modified to accept a formatted string as a 2nd parameter, in addition to the numerical constants (such as System date short) that can still be used.
Reminder: the String command can accept a date and an optional time!
String ( expression {; format {; addTime}} ) -> Function result
So now you can format dates and times with total freedom, even both at the same time!
$date:=!1954-03-17!
$time:=?13:24:35?
$format:="EEEE dd MMMM yyyy 'à' HH 'heures,' mm 'minutes et' ss 'secondes'"
$result:=string($date;$format;$time)
The result shall be “mercredi 17 mars 1954 à 13 heures, 24 minutes et 35 secondes” on a french system !
The “object set format” command
Based on the same principle, the command allows you to define the format of dates and times displayed in forms by programming. The syntax is exactly the same, as shown in the example below:
OBJECT SET FORMAT(*; "myDate"; "(yyyy) MMM dd") // (2017) Dec 12
OBJECT SET FORMAT(*; "mytime"; "hh'h' mm'mn' ss'sec.'") // 15h 12mn 17sec.
And finally, the interface!
Of course, everything that is programmed via the OBJECT SET FORMAT command can be done directly in the forms. The list of possible formats for dates and times now includes a “custom” line for entering the desired formatting string. Naturally, this string is constructed in the same way as the commands listed above.
New entry Pattern when the Date format is set to Custom
Conclusion
In short, this new feature gives you much freedom when displaying dates and times. Simplifying your applications’ code will reduce development time and enable better form optimisation. See the documentation for more details about this feature.