Calcul based on 2 dates

More
4 years 10 months ago #88134 by Ben_V
Ben_V created the topic: Calcul based on 2 dates
Hi,
I would like to set a (hidden or not) question calculating the number of nights between 2 dates (patient hospitalization delay):

q1 (date question type) => Admission
q2 (date question type) => Discharge

q3 (numerical/equation) => xDays

Is there any way to do this using EM or JS ?
Thanks in advance for any help!

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)

Please Log in to join the conversation.

More
4 years 10 months ago - 4 years 10 months ago #88157 by DenisChenu
DenisChenu replied the topic: Calcul based on 2 dates
Hello,

With 1.92, if you make date diff on a second page (to be sure to have SQL date).

You can try something like this:
var DateDiff = {
 
    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();
 
        return parseInt((t2-t1)/(24*3600*1000));
    },
 
    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();
 
        return parseInt((t2-t1)/(24*3600*1000*7));
    },
 
    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();
 
        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },
 
    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}
 
d1 = new Date({substr(DD,0,4)}, {substr(DD,5,2)}, {substr(DD,8,2)});
d2 = new Date({substr(D2,0,4)}, {substr(D2,5,2)}, {substr(D2,8,2)});
 
document.write("<br />Number of <b>days</b>: "+DateDiff.inDays(d1, d2));
document.write("<br />Number of <b>weeks</b>: "+DateDiff.inWeeks(d1, d2));
document.write("<br />Number of <b>months</b>: "+DateDiff.inMonths(d1, d2));
document.write("<br />Number of <b>years</b>: "+DateDiff.inYears(d1, d2));
Maybe there are other option with EM.

Denis
PS: DateDiff script from ditio.net/2010/05/02/javascript-date-difference-calculation/
PS2: DD is the code of the first date, D2 of the second date

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Last Edit: 4 years 10 months ago by DenisChenu. Reason: PS2

Please Log in to join the conversation.

More
4 years 10 months ago #88160 by Ben_V
Ben_V replied the topic: Calcul based on 2 dates
Merci Denis,
I will try to implement this asap and come back here for some feedback.

Thanks again.
Ben-

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)

Please Log in to join the conversation.

More
4 years 10 months ago - 4 years 10 months ago #88168 by Ben_V
Ben_V replied the topic: Calcul based on 2 dates
:cheer: working!
Now it will be great to add some validation
a) checking that DD date is always anterior than D2 (pretty important to avoid negative values)
b) setting a minimum starting date for DD, as "minimum year" does in advanced setting (could be current date )

Any idea?
Thanks

Note: LS original code for Minimum year:2012 & Maximum year:2014
<input  type='hidden' name='dateyearrange123X456X789' id='dateyearrange123X456X789' value='2012:2014'  />

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last Edit: 4 years 10 months ago by Ben_V.

Please Log in to join the conversation.

More
4 years 10 months ago #88202 by Ben_V
Ben_V replied the topic: Calcul based on 2 dates
I attach a sample export if somebody wants to play with it ;)

and 2 more or less related links to previous posts:
"Calculate age in an Equation type question, using Date of Birth"
"Default Date"

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Attachments:

Please Log in to join the conversation.

More
4 years 10 months ago #88233 by DenisChenu
DenisChenu replied the topic: Calcul based on 2 dates

benitov wrote: I attach a sample export if somebody wants to play with it ;) [/url]

If you have time to fill the workaround part of docs.limesurvey.org/ ;)

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
4 years 10 months ago #88237 by Ben_V
Ben_V replied the topic: Calcul based on 2 dates
Denis, no problem for completing the workaround but much better if i can previously find a solution regarding the validation (at least checking that 1st date is always anterior than 2nd one ...)

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)

Please Log in to join the conversation.

More
4 years 10 months ago - 4 years 10 months ago #88239 by DenisChenu
DenisChenu replied the topic: Calcul based on 2 dates
Hello,

Review after posting, if survey date is set to dd.mm.yyyy, then:
<script>
$("#limesurvey").submit(function(){
DD = $("#question7490 input.popupdate").val();
D2 = $("#question11621 input.popupdate").val();
if(substr(DD,6,4)<substr(D2,6,4)){ return true; }
if(substr(DD,3,2)<substr(D2,3,2)){ return true; }
if(substr(DD,0,2)<substr(D2,0,2)){ return true; }
alert("D2 must be less than DD");
$("#limesurvey .submit").attr("disabled",false);
return false;
});
</script>
AFter you can use some function like this:

A new question type "no answer" with:
<div class="error" id="myerror"></div>
<script>
function testifOK(){
D1 = $("#questionQQ1 input.popupdate").val();
D2 = $("#questionQQ2 input.popupdate").val();
if(D1==""){$("#myerror").text("You have to select a date for D1");return false;}
if(substr(D1,6,4)<substr(D2,6,4)){$("#myerror").text("The date for D1 must be less than D1");return false;}
...
}
*
....
* have to found a way to detect all change in datepicker

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Last Edit: 4 years 10 months ago by DenisChenu. Reason: *
The following user(s) said Thank You: Ben_V

Please Log in to join the conversation.

More
4 years 10 months ago #88297 by Ben_V
Ben_V replied the topic: Calcul based on 2 dates
Denis,

Can't get it working :(
Settings:
dd.mm.yyyy
Gr1: the 2 date pickers
Gr2: numerical (not hidden for the moment) question + boilerplate with the first script (only qid changes)
Gr3: ...other questions of the survey

I don't understand how to handle your second block of js :(
"A new question type "no answer" with..."

I think that I'll complete LS documentation right now... and updating it after in case of improvment...

Thank you for all

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)

Please Log in to join the conversation.

More
4 years 10 months ago #88364 by DenisChenu
DenisChenu replied the topic: Calcul based on 2 dates

benitov wrote: I don't understand how to handle your second block of js :(
"A new question type "no answer" with..."

Only the first part work ?

If yes, the second part is to launch some "information" at screen, but when i'm looking for blur or change , jquery ui calendar son't launch this .
Actually, you can do the job at "submit" only.


I have to found how to detect the date was changed.

Denis

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
4 years 10 months ago #88392 by Ben_V
Ben_V replied the topic: Calcul based on 2 dates
Hi Denis,
I attach the sample survey with a new approach directly based on the validation of my "ndays" variable (can't be negative value !)
Maybe this is not the pefect way but do the job... (could be improved by an alert or confirm box with direct return to previous page )

What's your opinion?

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Attachments:

Please Log in to join the conversation.

More
4 years 10 months ago - 4 years 10 months ago #88792 by Ben_V
Ben_V replied the topic: Calcul based on 2 dates

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last Edit: 4 years 10 months ago by Ben_V.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

More
4 years 10 months ago #88800 by tpartner
tpartner replied the topic: Calcul based on 2 dates
Thanks Ben.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
4 years 10 months ago #88839 by DenisChenu
DenisChenu replied the topic: Calcul based on 2 dates

tpartner wrote: Thanks Ben.

+1 ;)

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now