Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Calcul based on 2 dates

Calcul based on 2 dates 3 years 2 months ago #88134

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1593
  • Thank you received: 391
  • Karma: 103
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
The administrator has disabled public write access.

Calcul based on 2 dates 3 years 2 months ago #88157

  • DenisChenu
  • DenisChenu's Avatar
  • Online
  • Moderator Lime
  • Posts: 8208
  • Thank you received: 1119
  • Karma: 331
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
Last Edit: 3 years 2 months ago by DenisChenu. Reason: PS2
The administrator has disabled public write access.

Calcul based on 2 dates 3 years 2 months ago #88160

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1593
  • Thank you received: 391
  • Karma: 103
Merci Denis,
I will try to implement this asap and come back here for some feedback.

Thanks again.
Ben-
Benoît
The administrator has disabled public write access.

Calcul based on 2 dates 3 years 2 months ago #88168

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1593
  • Thank you received: 391
  • Karma: 103
: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
Last Edit: 3 years 2 months ago by Ben_V.
The administrator has disabled public write access.

Calcul based on 2 dates 3 years 2 months ago #88202

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1593
  • Thank you received: 391
  • Karma: 103
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"
Attachments:
Benoît
The administrator has disabled public write access.

Calcul based on 2 dates 3 years 2 months ago #88233

  • DenisChenu
  • DenisChenu's Avatar
  • Online
  • Moderator Lime
  • Posts: 8208
  • Thank you received: 1119
  • Karma: 331
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/ ;)
The administrator has disabled public write access.

Calcul based on 2 dates 3 years 2 months ago #88237

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1593
  • Thank you received: 391
  • Karma: 103
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
The administrator has disabled public write access.

Calcul based on 2 dates 3 years 2 months ago #88239

  • DenisChenu
  • DenisChenu's Avatar
  • Online
  • Moderator Lime
  • Posts: 8208
  • Thank you received: 1119
  • Karma: 331
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
Last Edit: 3 years 2 months ago by DenisChenu. Reason: *
The administrator has disabled public write access.
The following user(s) said Thank You: Ben_V

Calcul based on 2 dates 3 years 2 months ago #88297

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1593
  • Thank you received: 391
  • Karma: 103
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
The administrator has disabled public write access.

Calcul based on 2 dates 3 years 2 months ago #88364

  • DenisChenu
  • DenisChenu's Avatar
  • Online
  • Moderator Lime
  • Posts: 8208
  • Thank you received: 1119
  • Karma: 331
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
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.191 seconds