WebFOCUS: Simplifying Date Formatting

If you’re a software developer using popular languages such as C# and Java, you’re used to a lot of string formatting options for dates.

For example, in C#, this line of code will look familiar.

DateTime thisDate = new DateTime(2014,1,1);

string date_str=thisDate.ToString(“MMMM dd, yyyy”);

The variable date_str will look like this: January 1, 2014

Unfortunately, I wasn’t able to find something similar in WebFOCUS. There a lot of date functions to choose from, but that’s a double edge sword; you can do a lot of with dates which gives you flexibility, but it can be confusing when to use what functions and makes it difficult to remember.

I created a custom function that simplifies this task of turning dates into strings (ALPHAs). You can download this WebFOCUS date utility. Download WebFOCUS date utility file

Here’s an example of how you can use it:

-INCLUDE date_utils.fex

TABLE FILE CAR
PRINT
COMPUTE DATETIME/HYYMDs =‘2014-01-02 23:58:59’;
COMPUTE Text2/A100 = DATETIME.TOSTRING(DATETIME,‘MMM YY’);

COMPUTE DATE/YYMD = ‘2013-01-03’;
COMPUTE Test/A100 = DATE.TOSTRING(DATE,‘MMM ”YY’);
BY CAR
END
Here are all the available formats you can use in my custom function – everything is case sensitive!
YYYY = four digit year; e.g. 2014
YY = two digit year; e.g. 14
MMMM = full month name; e.g. February
MMM = three letter month name; e.g. Feb
MM = two digit month padded with a 0; e.g. 02
M = varying 1-2 digit month; e.g. 2
DD = two digit day padded with a 0; e.g. 01
D = varying 1-2 digit day; e.g. 1

Of course, much more can be done, but this covers the majority of the use cases that I typically run into.

Feel free to extend as you need. If you need help, extending the functionality, shoot me an email at: sandy@alexsandy.com

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

Current day month ye@r *