DEV Community

Peter + AI
Peter + AI

Posted on

Understanding the `datetime` Data Type in Uniface 10.4 πŸš€

Hey everyone! πŸ‘‹ If you're working with the Uniface platform, you know that handling dates and times is a fundamental task. Today, let's take a closer look at a key data type for this job: datetime.

Quick note: This blog post was created with the help of an AI to explain the official Uniface 10.4 documentation in a simple way.

What is datetime? πŸ€”

In Uniface, datetime is a special data type designed to hold both a date and a time in a single variable. It follows a very specific internal format:

ccyymmddhhnnsstt
Enter fullscreen mode Exit fullscreen mode

Let's break that down:

  • cc: century
  • yy: year
  • mm: month
  • dd: day
  • hh: hour
  • nn: minute
  • ss: second
  • tt: hundredths of a second

You can use this data type when you define variables in your ProcScript code, or even when modeling fields in your application components.

How to Declare a datetime Variable

Using it in your code is straightforward. Inside a variables block, you just declare it like this:


variables
  datetime V_MY_TIMESTAMP
  datetime V_ORDER_DATE
endvariables
Enter fullscreen mode Exit fullscreen mode

Now, V_MY_TIMESTAMP is ready to hold a full date and time value!

The "Magic" of Default Values ✨

Here's where datetime gets interesting. Uniface is quite smart about how it handles incomplete or missing values. Here are the rules:

  • If you provide no value at all (neither date nor time): The value is treated as NULL.

  • If you only provide a time: The date automatically defaults to the current date.

  • If you provide an incomplete date:

    • A missing day defaults to 1 (the first of the month).
    • A missing month defaults to the current month.
    • A missing year defaults to the current year.

Converting Data with the $datim Function

Often, you'll have a date/time as a string or a number and need to convert it into a proper datetime type. For that, Uniface gives us the $datim function. It's your best friend for this task.

Here’s a simple example:


variables
  string V_DATE_STRING
  datetime V_CONVERTED_DATETIME
endvariables

; Let's say our string is a date
V_DATE_STRING = "20251102"

; Convert the string to a datetime format
V_CONVERTED_DATETIME = $datim(V_DATE_STRING)

; After this line, V_CONVERTED_DATETIME will hold '2025110200000000'
; The time part defaults to midnight because we didn't specify it.
Enter fullscreen mode Exit fullscreen mode

Conclusion

And that's the basics of the datetime data type in Uniface 10.4! It's a powerful and flexible type for managing date and time information, especially with its smart default behaviors. Happy coding! πŸ‘

Top comments (0)