Focal Point
[SHARING] Flexible input date handling

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/6937058096

October 16, 2018, 05:47 AM
Wep5622
[SHARING] Flexible input date handling
I created a few DEFINE FUNCTIONs to handle different date input formats from calendar controls and the like. The basic trick is to recognize the input format by its pattern and convert it accordingly. Having a pattern available, we also suddenly have the possibility of using DECODE to choose suitable conversion formats! Me likes...

We always use YYMD formats internally (as opposed to DMYY or MDYY), we find them convenient (and less confusing), which is why there's a function specifically converting to that output.

The output format is always without dividers (hyphens or slashes), but that omission can (probably) easily be amended with a few more functions Wink

-* Convert a given date to the desired date format.
-* For example: '01-10-2018' to format '9999-99-99'
DEFINE FUNCTION DATE2FMT(INDATE/A10, OUTFMT/A8)
	PAT/A10	= PATTERN(10, INDATE, PAT);
	MASK/A10	= DECODE PAT(
			'99-99-9999' '99$99$9999'
		,	'99/99/9999' '99$99$9999'
		,	'99999999' '99999999'
		ELSE	'9999$99$99'
	);
	FMT/A6	= DECODE PAT(
			'99-99-9999' 'A8DMYY'
		,	'99/99/9999' 'A8MDYY'
		ELSE	'A8YYMD'
	);
	STR/A10 = EDIT(INDATE, MASK);
	DATE2FMT/A10 = DATECVT(STR, FMT, OUTFMT);
END
-RUN


-* Automatically convert a given date to standard format (YYMD)
DEFINE FUNCTION DATE2STD(INDATE/A10)
	DATE2STD/A10 = DATE2FMT(INDATE, 'A8YYMD');
END
-RUN

-* Convert a given date to the desired date pattern.
DEFINE FUNCTION DATE2PATTERN(INDATE/A10, OUTPAT/A10)
	FMT/A6 = DECODE OUTPAT(
			'99-99-9999' 'A8DMYY'
		,	'99/99/9999' 'A8MDYY'
		ELSE 'A8YYMD'
	);
	DATE2PATTERN/A10 = DATE2FMT(INDATE, FMT);
END
-RUN


Additions, corrections, etc. are of course welcome.


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
October 16, 2018, 11:41 AM
FP Mod Chuck
Good One


Thank you for using Focal Point!

Chuck Wolff - Focal Point Moderator
WebFOCUS 7x and 8x, Windows, Linux All output Formats