KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Generating a Filename-Safe Timestamp
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: August 7, 2025

When a process is created that generates files is used more than once, the naming convention of the files can be important. A typically used naming convention is to add a timestamp to the name, this allows the name of the file to be fairly unique and informative at the same time.

The 4D Timestamp command returns the current UTC time in ISO format:
yyyy-MM-ddTHH:mm:ss.SSSZ
Unforturnately the ISO format contains some characters that are not valid for file names. The most important ones are the colins and period.
The following code strips these characters from the timestamp by utilizing the Replace string command:

$timeStamp:=Replace string(Replace string(Timestamp; ":"; ""); "."; "")

This will return the timestamp without the invalid characters in the following format:
yyyy-MM-ddTHHmmssSSSZ

Depending on the use-case, the timestamp can be manipulated in what makes the most sense. For example, if a shorter name was desired then the following could be used:
$timeStamp:=Replace string(Replace string(Replace string(Substring(Timestamp; 1; 19); "-"; ""); ":"; ""); "T"; "_")

This removes everything except for the date and significant time:
yyyyMMddHHmmss

When the timestamp is formatted in a desireable way, it can then be added to a filename, such as appending it to a report:
$filename:="MonthlyReport_"+$timestamp

This allows the Timestamp command to return the current date and time as a string without calling many other 4D commands, and then any undesired characters can simply be removed or replaced using string manipulation commands.