Focal Point
SOLVED: How to convert yyyy/mm/dd string value to date value in dialog manager?

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

May 02, 2013, 05:39 PM
JC Zhu
SOLVED: How to convert yyyy/mm/dd string value to date value in dialog manager?
I have a variable in dialog manager that contains date in the format of 'yyyy/mm/dd'. How can I convert it to a date value so that I can apply the DATEADD function to it? I have searched in the forum but I have only found examples that work when the date value is coming from TABLE FILE. I have not been able to find examples that work in dialog manager.

This message has been edited. Last edited by: JC Zhu,


WebFOCUS 7.7.3
Windows, All Outputs
May 02, 2013, 07:41 PM
Alan B
It depends as to what you are trying to use DATEADD for. If you need to use Weekdays or Business days, then DATEADD is good:
-SET &INPUTDATE = '2013/05/03';
-SET &NEWDATE =  HCNVRT(HDTTM(DATEADD(HDATE(HINPUT(8,EDIT(&INPUTDATE,'9999$99$99'),8,'HYYMDS'),'YYMD'),'WD',5),8,'HYYMDS'), '(HYYMD)', 10, 'A10');
-*                                                                                                     ------
-TYPE &NEWDATE

WD - Weekday, 5 for increment by 5. BD for business days using SET BUSDAYS.

If you want to use just days, months or years, HADD will work:

-SET &NEWDATE =  HCNVRT(HADD(HINPUT(8,EDIT(&INPUTDATE,'9999$99$99'),8,'HYYMDS'),'dd',5,8,'HYYMDS'),'(HYYMD)',10,'A10');
-*                                                                              ------
-TYPE &NEWDATE

dd - days, 5 for increment by 5. Check documentation for other options.


Alan.
WF 7.705/8.007
May 02, 2013, 09:16 PM
Dan Satchell
If you're just trying to add or subtract days from the initial date value, you can also strip out the slashes with EDIT and use the legacy function AYMD. With AYMD, conversion to a SmartDate value is not necessary.


WebFOCUS 7.7.05
May 03, 2013, 09:50 AM
JC Zhu
What if the variable has a single digit month or day? For example, 2013/5/2. How can that be handled?


WebFOCUS 7.7.3
Windows, All Outputs
May 03, 2013, 10:27 AM
Alan B
It's more difficult! Referring to previous post, the code becomes:
-SET &INPUTDATE = '2013/5/1';
-SET &YEAR = GETTOK(&INPUTDATE,&INPUTDATE.LENGTH,1,'/',4,'A4');
-SET &MONTH = GETTOK(&INPUTDATE,&INPUTDATE.LENGTH,2,'/',2,'A2');
-SET &DATE = GETTOK(&INPUTDATE,&INPUTDATE.LENGTH,3,'/',2,'A2');
-SET &NEWDATE =  HCNVRT(HDTTM(DATEADD(HDATE(HSETPT(HSETPT(HSETPT(HGETC(8,'HYYMDS'),'year',&YEAR,4,'HYYMDS'),'month',&MONTH,2,'HYYMDS'),'dd',&DATE,2,'HYYMDS'),'YYMD'),'WD',2),8,'HYYMDS'),'(HYYMD)',10,'A10');
-*                                                                                                                                                                    ------
-TYPE &NEWDATE

or
-SET &INPUTDATE = '2013/5/1';
-SET &YEAR = GETTOK(&INPUTDATE,&INPUTDATE.LENGTH,1,'/',4,'A4');
-SET &MONTH = GETTOK(&INPUTDATE,&INPUTDATE.LENGTH,2,'/',2,'A2');
-SET &DATE = GETTOK(&INPUTDATE,&INPUTDATE.LENGTH,3,'/',2,'A2');
-SET &NEWDATE =  HCNVRT(HADD(HSETPT(HSETPT(HSETPT(HGETC(8,'HYYMDS'),'year',&YEAR,4,'HYYMDS'),'month',&MONTH,2,'HYYMDS'),'dd',&DATE,2,'HYYMDS'),'dd',4,8,'HYYMDS'),'(HYYMD)',10,'A10');
-*                                                                                                                                              ------
-TYPE &NEWDATE



Alan.
WF 7.705/8.007
May 03, 2013, 11:10 AM
DBADiaz
It is important to note that Dialogue Manager interprets dates as alphanumeric or numeric, and the date functions such as DATEADD requires a standard date stored as an offset from the base date.
What this means is you have to convert the date string to a date offset using the function DATECVT.
Here is a sample:
  
-* Initial Date string
-SET &MYDATE = '2012/01/01';

-*Removing the '/' from the date
-SET &MYDATE = EDIT(&MYDATE, '9999$99$99');

-*Converting the date to an offset
-SET &MYDATE_OFFSET = DATECVT(&MYDATE, 'I8YYMD', 'YYMD');

-* Now you can use the &MYDATE_OFFSET with most of the date manipulation functions, such as DATEADD and DATEMOV



Release: DevStudio 7703
OS: Win7
Outputs: HTML, PDF, EXCEL

May 03, 2013, 11:14 AM
JC Zhu
Thanks for the answers. I have just found that I can more easily achieved that with passing the variable to the DATEADD function directly without any conversion. I tried this initially but it didn't work. I got this to work by adding single quotes around the variable name when passing the value to the DATEADD function. With the single quotes, DATEADD seems to be able to automatically interpret it as a date.


-SET &INPUTDATE = '2013/5/2';
-SET &NEWDATE = HCNVRT(HDTTM(DATEADD('&INPUTDATE', 'D', -1),8,'HYYMDS'),'(HYYMD)',10,'A10');
-TYPE &NEWDATE


WebFOCUS 7.7.3
Windows, All Outputs
May 03, 2013, 01:14 PM
Alan B
quote:
With the single quotes, DATEADD seems to be able to automatically interpret it as a date.


Not in my experience it won't, nor according to the documentation.


Alan.
WF 7.705/8.007