DesignBais Tip – Formatting Date/Time Fields
For fields that input and display time, the default output format is MTHS, which will show AM/PM plus the seconds. Most applications don’t require seconds, but there is no way to display data in DesignBais with a mask like MTH. A similar situation applies to dates where you want to input or display dates in a variety of formats, depending on circumstances. This article goes into detail about how to do this, code included.
To get data in MTH format you need to use a work field which updates the real DBRECORD field from code.
- Create a work field and add it to your form. Set the Process After spec to call your event handling code, and put “MTH” in the Parameters field.
- Add the real field for the time data that will actually be filed.
- Set the Section for this real field to “Hide” and then under Sections, set the Hide section to be hidden. Do this for all such fields to ensure that you have time fields displayed only once on the form, represented by work fields.
- Merge and modify the sample validation code provided on the next page of this article into your event handling code. The Validate.Date.Time code can be used as-is (subject to MV platform differences).
- The code processes data in the work fields. If after doing the validation, both the internal/input-format data and the output-formatted data are valid, then set the hidden time fields in DBRECORD (or whichever buffer) to be the final input-format value, and display the output format in the work field – this of course is the MTH formatted output you wanted in the first place.
- This technique can be used to format any data as you want it in the browser, while still saving the internally formatted data properly in the data files.
About the sample code:
- This particular piece handles From/To Time ranges.
- For readability it is intentionally more verbose than it needs to be.
- It accepts the following formats:
- Single digit and two-digit numeric = hours.
- Examples: 1,2,3,12,13,14
- Three to four digit numeric is hours:minutes.
- Examples:
- 130 = 01:30am
- 945 = 09:45am
- 1330 = 13:30 = 01:30pm
- Examples:
- Standard 24 hour formats as hh:mm
- Long format as hh:mmAM or hh:mmPM
- Single digit and two-digit numeric = hours.
- This does not check for nulls or to ensure that the end-time comes after the start-time. That sort of checking is an application-specific exercise left to the developer.
The code follows on the next page.