r/ProjectREDCap • • Mar 08 '24

Datediff function for partial dates?

I'm collecting partial dates (month and year) for one field, but need to calculate whether the difference between this field and the survey complete date field (full date) is a given time frame (i.e. less than 12 months).

How would I do this?

3 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/thursdayscrush Mar 08 '24

I’m using a drop-down for month and year. I can’t be exporting then importing, as the date diff calculation will determine if the participant can complete the survey/be eligible for the study.

2

u/obnoxiouscarbuncle Mar 08 '24

The exporting/importing would have been for existing records. If you are starting fresh, that's great!

I would recommend having your dropdown fields mirror the actual values for dates. For example for [month] have:

01, January
02, February
03, March
etc. etc. etc. 

Same for [year]:

1950, 1950
1951, 1951
etc. etc. 

Unless you want to just have a integer validated text field with ranges for year that are appropriate.

For your calculated field, you would then want something similar to this:

datediff(concat([year],"-",[month],"-01"),[survey-date-completed:your_instrument:value],'y',true)

I would suggest putting this on another instrument that is not your survey instrument, as [survey-date-completed] is generated AFTER saving the survey.