dateadd function

Discussions about CSEntry
Post Reply
axel
Posts: 5
Joined: June 16th, 2019, 7:07 pm

dateadd function

Post by axel » June 16th, 2019, 7:13 pm

hi
i want to use dateadd function but with another date format like (DDMMYYYY)
this is because the argument in the function is taken from another item


PROC FIN_DE_LA_SEMAINE
preproc
FIN_DE_LA_SEMAINE=dateadd(DEBUT_DE_LA_SEMAINE,7,"d") ;
noinput;

I know that if I change DEBUT_DE_LA_SEMAINE format to "YYYYMMDD", it is ok
but I want to display both as "DDMMYYYY"

Can someone help me with that?

josh
Posts: 1916
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: dateadd function

Post by josh » June 17th, 2019, 12:17 am

The dateadd function only works with YYYYMMDD format. If your date is in DDMMYYYY format you will have to convert it to YYYYMMDD . There are a few ways to do that.

You can use division and modulus to extract the year, month and day from the date and then rebuild the date in the format you want. For example here are a couple of functions you can use to convert between DDMMYYYY and YYYYMMDD formats:
function ddmmyyyyToyyyymmdd(ddmmyyyy)
   
numeric year = ddmmyyyy % 10000;
   
numeric month = int(ddmmyyyy/10000) % 100;
   
numeric day = int(ddmmyyyy/1000000);
    ddmmyyyyToyyyymmdd = year *
10000 + month * 100 + day;
end;

function yyyymmddToddmmyyyy(yyyymmdd)
   
numeric year = int(yyyymmdd/10000);
   
numeric month = int(yyyymmdd/100) % 100;
   
numeric day = yyyymmdd%100;
    yyyymmddToddmmyyyy = day *
1000000 + month * 10000 + year;
end;
You could use these in your logic like this:
PROC FIN_DE_LA_SEMAINE
preproc
FIN_DE_LA_SEMAINE=yyyymmddToddmmyyyy(dateadd(ddmmyyyyToyyyymmdd(DEBUT_DE_LA_SEMAINE),7,"d"));
noinput;
Another way to do the same date format conversion is to convert your date to a string using the maketext function and then use substrings (the [] operator) to extract the day, month and year. Then convert back to a number using the tonumber() function to pass it to dateadd. Finally convert the result of dateadd to string, extract the day, month and year and use maketext and tonumber to convert the result back to the original format.

My preferred solution is to modify your date variables in the dictionary to have sub-items for the day, month and year. Then you can put only the sub-items on the form rather than putting the items themselves on the form. This way you can change the order of the day, month and year fields on your form to the order that you want to display to the user - DDMMYYYY in your case. The order that you put the fields on the form does not affect how the data is stored so you could have your date in YYYYMMDD format in the dictionary but have it in DDMMYYYY format on the form. That way you could use the dateadd function with the item without changing the format but it will displayed in the format you want on the form.

axel
Posts: 5
Joined: June 16th, 2019, 7:07 pm

Re: dateadd function

Post by axel » June 17th, 2019, 5:01 am

Thank you, Josh!

It is working very well!
I am using the functions in my logic.

I want to know if it is possible to use "Field properties Capture Type (Date)" in your preferred solution.
Because I want to make it easier for the interviewer to choose the start date with a calendar
instead of having to remember it.

Again thank you for these solutions
You save me a lot of time!

Gregory Martin
Posts: 1209
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: dateadd function

Post by Gregory Martin » June 17th, 2019, 6:45 am

You can add an eight-digit item to the form and set the property date capture format YYYYMMDD and you'll be able to use it easily with the datediff/dateadd/datevalid functions. If you want it in a different format than yyyy/mm/dd, you can collect it like that, but then you'll have to transform it to a standard format to use in the functions.

axel
Posts: 5
Joined: June 16th, 2019, 7:07 pm

Re: dateadd function

Post by axel » June 17th, 2019, 7:42 am

Thanks a lot Gregory Martin

Post Reply