KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility to return a properly formated date regardless of OS Region setting
PRODUCT: 4D | VERSION: 13.2 | PLATFORM: Mac & Win
Published On: May 21, 2013

There are situations where a database will be required to produce a date in a region of the world with a date display format different from the initial programming region. A perfect example for that is the United State where the date display formate is MM/DD/YYYY and Canada where the date display formate is YYYY-MM-DD.

For dates stored in fields 4D automatically handles the proper display of dates on forms. The issue of producing a properly displayed date is when dates are being constructed programmatically. Instead of trying to figure out what region the database is in and how to properly construct a date, it is easy to let 4D do it. The utility provided below takes three parameters, the year, the month, and day, and produce the correctly formatted date regardless of the region an OS is set for. The utility takes advantage of the built-in feature of the 4D command Add to date to return a correctly formatted date for the current OS region setting.

If (True)
   If (False)
       //*****************************************************************************
       //
       // UTIL_FormattedDate
       //
       // Purpose: Return a date in the format of the current region setting
       //
       // $0 - Date - Returned formatted date
       // $1 - Longint - The year
       // $2 - Longint - The month
       // $3 - Longint - The day
       //
       //*****************************************************************************
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
   C_DATE($0;$Date_D)
   C_LONGINT($1;$Year_L)
   C_LONGINT($2;$Month_L)
   C_LONGINT($3;$Day_L)
    //--------------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------------
    //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L)
   C_TEXT($Format_T;$Delimiter_T)

End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters

If($Params_L=3)

   $Year_L:=$1
   $Month_L:=$2
   $Day_L:=$3

    //======================== Method Actions ==================================

   $Date_D:=Add to date(!00/00/00!;$Year_L;$Month_L;$Day_L)

    //======================== Clean up and Exit =================================

End if

$0:=$Date_D