DEV Community

Peter + AI
Peter + AI

Posted on

πŸ“… Working with Dates in Uniface 10.4: The $date Function Explaine

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 ) }
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🌍 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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

⚠️ 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 $procerror for 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)