KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: 4D v11 SQL ISO compatible Date/Time Stamp
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: January 21, 2009

Many times a developer has the need for a "Date/Time Stamp" to store in a field. Instead of adding a Date and a Time field, consider this alternative. Whereas 4D does not have a native type of "DATETIME", such as many other SQL databases do, it does have the tools for constructing an ISO 8601 compatible date/time stamp, minus the time zone declaration.

C_STRING(19;$0) `// returns a string in the ISO 8601 format: "YYYY-MM-DDTHH:MM:SS"
$0:=Substring(String(Current date;ISO Time );1;11)+Substring(String(Current time;ISO Time );12)


A nice advantage of of this format is its search- and sort-ability. If the field is indexed it lends itself very well to be searched and sorted on.

Its drawback is the data size. Whereas the this field requires 42 bytes of 4D storage space, the SQL type requires only 8 bytes, since it stored in the equivalent of a 4D type of C_REAL.

However, there is also a drawback to the SQL Date/Time type that this method does not have. The SQL type has a min and max date because of the size limitations of the type. There is not min or max date limitation using the technique shown here.

Commented by Charlie Vass on June 3, 2009 at 8:23 AM
To refine this method even further consider the following:

Replace string(String(Current date;ISO Date );"00:00:00";String(Current time))