This blog post was created with the assistance of AI to help developers understand Uniface date handling. π€
π What is the $date Function?
The $date function in Uniface 10.4 is a powerful tool that handles two main tasks:
- Getting the current system date
- Converting string values to proper Date data type
This function is essential for any Uniface developer working with date values in their applications. π
π‘ Basic Syntax
The syntax is simple and flexible:
$date { ( Source ) }
The Source parameter is optional - when you don't provide it, you get today's date! π
π§ How It Works
Without Parameters
When you call $date without any parameters or with an empty string, it returns the current system date:
$current_date = $date
// Returns today's date
With String Conversion
When you provide a string, it tries to convert it to a Date data type:
$converted_date = $date("21-05-2019")
// Converts the string to a proper date
π Locale Matters!
One of the most important things to understand is that $date behaves differently depending on your locale settings. There are two main modes:
Classic Mode
In classic mode, Uniface is very forgiving and tries hard to understand your date string. The standard format is dd-mmm-yyyy.
Locale-Specific Mode
In locale mode, strict rules apply based on your selected locale:
- πΊπΈ US English:
mmm dd, yyyy(May 21, 2019) - π©πͺ German:
dd.mm.yyyy(21.05.2019) - π³π± Dutch:
dd mmm yyyy(21 mei 2019)
π Real Examples
Let's look at some practical examples. Assuming the current date is May 21, 2019:
// Getting current date
clrmess
$1 = $date("1-2-94")
putmess "$1 = %%$1 on value of 1-2-94"
$2 = $date
putmess "$2 = %%$2 on value of null"
With default format dd-mmm-yyyy, this produces:
$1 = 01-feb-1994 on value of 1-2-94
$2 = 21-aug-1994 on value of null
But with mmm-dd-yyyy format, the same code gives:
$1 = jan-02-1994 on value of 1-2-94
$2 = aug-21-1994 on value of null
β οΈ Error Handling
When something goes wrong, the function handles errors gracefully:
- Invalid strings return empty string
"" - Error code -1004 (
<UPROCERR_DATE>) indicates invalid date value - Check
$procerrorfor specific error information
π― Best Practices
1. Know Your Locale
Always be aware of your locale settings when working with date conversions. Different locales expect different formats! π
2. Handle Errors
Always check for empty returns and use $procerror to handle conversion failures properly.
3. Use Consistent Formats
If you're working with external date strings, make sure they match your expected locale format or convert them appropriately.
π Server vs Client
Important note for distributed applications: when running in remote environments, $date returns the server's system time, not the client's! This is crucial for multi-user applications. π₯οΈ
β When to Use $date
The $date function is allowed in all Uniface component types, making it versatile for:
- Form components
- Service components
- Report components
π Conclusion
The $date function is a fundamental tool in Uniface development. Understanding how it works with different locales and handling its potential errors properly will make your date-handling code more robust and reliable.
Remember: always test your date conversions with different locale settings if your application will be used internationally! π
Happy coding with Uniface 10.4! π¨βπ»π©βπ»
*** **Keywords:** uniface procscript date conversion locale
Top comments (0)