days between dates

Viewing 13 reply threads
  • Author
    Posts
  • February 18, 2023 at 12:20 PM #48948

    Glen Forister
    Participant

    I did some study and tried to determine the #days between the date of one record and the previous record. I can’t get past line one syntax. Help please?

    
    Function Days_Between_Dates(){
    
    	var date_id = 'fld-119ad5683104451ca6855e777a23ad21');
    
    	
    	var records = form.getRecords();   // calls function getRecords()
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousday = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
        	
        var today = record.getFieldValue(date_id);
        var previous_day = previousRecord.getFieldValue(date_id);
        var total = today - previousday;
        return total;
        
    }
    
    February 18, 2023 at 8:41 PM #48950

    Daniel Leu
    Participant

    lower case: function...

    February 19, 2023 at 10:20 AM #48954

    Glen Forister
    Participant

    Ok, missed that.
    But I got all the syntax cleared and this script does nothing, not even printing to console the variable values. So I’ve proved I haven’t learned anything yet.
    Any ideas?
    Thanks.

    
    function Days_Between_Dates(){
    	var date_id = ('fld-119ad5683104451ca6855e777a23ad21');
    
    	
    	var records = form.getRecords();   // calls function getRecords()
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousday = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
        	
        var today = record.getFieldValue(date_id);
        var previous_day = previousRecord.getFieldValue(date_id);
        var total = today - previousday;
        
        
        console.log("today: ")
    	console.log("previousday: ")
    	console.log("total: ")
        
        return total;
    }
    
    
    February 19, 2023 at 4:51 PM #48959

    Daniel Leu
    Participant

    Do you have a Days_Between_Dates(); at the end of the script after the closing bracket of the code you showed? This causes your function to be called.

    If you run this script on the first record, it will not do anything since it is the first record.

    Sometimes I add something like this to my functions:

    function Days_Between_Dates(){
       console.log("Days_Between_Dates() called ...");
       ...
    }
    
    Days_Between_Dates();

    This way I can easier trace what’s going on with my scripts.

    February 20, 2023 at 11:42 AM #48962

    Glen Forister
    Participant

    I added that. It just prints to the screen after every recored looked at I guess. I don’t get it.
    There aree 26 records.

    Here is the result.
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    2/20/23, 10:38:59 AM / Psoriasis / Days since last Trt.
    Days_Between_Dates() called …
    ————-

    function Days_Between_Dates(){
    	var date_id = ('fld-119ad5683104451ca6855e777a23ad21');
    	var records = form.getRecords();   // calls function getRecords()
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousday = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
        	
        var today = record.getFieldValue(date_id);
        var previous_day = previousRecord.getFieldValue(date_id);
        var total = today - previousday;
        
        console.log("today: ")
    	console.log("previousday: ")
    	console.log("total: ")
        
        return total;
    }
    function Days_Between_Dates(){
       console.log("Days_Between_Dates() called ...");
      
    }
    
    Days_Between_Dates();
    • This reply was modified 1 year, 2 months ago by Glen Forister.
    • This reply was modified 1 year, 2 months ago by Brendan.
    February 20, 2023 at 5:26 PM #48965

    Brendan
    Keymaster

    I’m a bit confused with your script. Did you define Days_Between_Dates() twice?

    Or was that a typo in your post?

    I also edited your post to put the back-ticks in so your code was easier to read.

    • This reply was modified 1 year, 2 months ago by Brendan.
    February 20, 2023 at 5:31 PM #48968

    Brendan
    Keymaster

    I see what you did. You copied Daniel’s code and put it inside your script after you had already defined the function.

    What he meant by this part:

    function Days_Between_Dates(){
       console.log("Days_Between_Dates() called ...");
       ...
    }

    Was take your existing script and just below the function declaration, put in the console.log() call. Not duplicate the function definition again.

    Then after your function definition, actually call the Days_Between_Dates() function which your original script did not show you were doing.

    February 20, 2023 at 7:12 PM #48971

    Glen Forister
    Participant

    Ok, Tried that.
    End result = Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    2/20/23, 6:24:40 PM / Psoriasis / Days since last Trt.
    Days_Between_Dates()
    Days since last Trt.: ReferenceError: Can’t find variable: previousRecord, line:(null)

    I’ve done lots of Basic, some Pascal, and some R in the past, but this is very different and I’m lost. Went through the basic training, but still new. It will take lots of experimenting and work to get to where I need to be to fix this. In time I will I hope, but still in the process of making TF usable to me before I branch off into another programming language.

    PS, Have no idea what the “called…” after in the Console.log statement does. I just get a syntax error.

    function Days_Between_Dates(){

    console.log(“Days_Between_Dates()”);
    var date_id = (‘fld-119ad5683104451ca6855e777a23ad21’);

    var records = form.getRecords(); // calls function getRecords()
    var currentRecordIndex = records.indexOf(record);

    if (currentRecordIndex > 0) {
    var previousday = records[currentRecordIndex-1];
    } else {
    return 0;
    }

    var today = record.getFieldValue(date_id);
    var previous_day = previousRecord.getFieldValue(date_id);
    var total = today – previousday;

    console.log(“today: “)
    console.log(“previousday: “)
    console.log(“total: “)

    return total;
    }
    Days_Between_Dates()

    Thanks for looking, but…

    February 20, 2023 at 8:55 PM #48974

    Daniel Leu
    Participant

    I got several errors when I tried to run your code. This touched-up version works for me:

    var date_id = 'fld-xxx';
    
    function Days_Between_Dates(){
    	console.log("Days_Between_Dates() called ...");
    
    	var days = -1;
    	var records = form.getRecords(); // calls function getRecords()
    	var currentRecordIndex = records.indexOf(record);
    
    	if (currentRecordIndex > 0) {
    		var previousRecord = records[currentRecordIndex-1];
    	} else {
    		return -1;
    	}
    
    	// get the two dates
    	const today = record.getFieldValue(date_id);
    	const previous_day = previousRecord.getFieldValue(date_id);
    	
    	// calculate the difference between the two date objects
    	days = Math.round((today - previous_day) / (1000 * 60 * 60 * 24));
    
    	console.log("today: " + today)
    	console.log("previous day: " + previous_day)
    	console.log("difference: " + days)
    	
    	return days;
    }
    
    Days_Between_Dates()

    In Javascript, the Date object represents the time in milliseconds since January 1, 1970, UTC. So the difference needs to be divided by milliseconds-per-day.

    I changed the return value to -1 for the first record.

    When reporting a syntax error, it’s helpful to have the entire message.

    When posting code, please encapsulate it in back-ticks. This way it is easier to copy&paste it and it looks prettier. Thanks!

    Cheers & happy hacking!

    • This reply was modified 1 year, 2 months ago by Daniel Leu.
    February 21, 2023 at 12:09 PM #48979

    Glen Forister
    Participant

    Wow, it really gets complicated to get that last date!
    I don’t see why it runs for you and not for me.
    I have the Result Type = Number, so that isn’t it.
    I attached the archve and maybe you can tell what I’m doing wrong.
    It sure has a problem with “today”.

    Days_Between_Dates() called …
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN
    2/21/23, 11:03:42 AM / Psoriasis / Days since last Trt.
    Days_Between_Dates() called …
    today: undefined
    previous day: undefined
    difference: NaN

    Attachments:
    You must be logged in to view attached files.
    February 21, 2023 at 4:39 PM #48985

    Brendan
    Keymaster

    You need to put in your own field ID instead of fld-xxx as in Daniel’s sample script. He doesn’t know what your field ID is, so we often just put in stand-ins for a field ID. It’s something you have to substitute yourself when you see any code here. Field IDs are unique to your own forms.

    February 21, 2023 at 5:10 PM #48986

    Glen Forister
    Participant

    Of course. How did I miss that.
    It does work.
    I have some work to do to understand the scripts you have given me, so thanks for that.

    Also, thanks for the calc info “In Javascript, the Date object represents the time in milliseconds since January 1, 1970, UTC. So the difference needs to be divided by milliseconds-per-day.”

    I will need that in another Form I just used the above script on and it worked. I have to know the weeks and months, not just days.

    February 21, 2023 at 8:15 PM #48988

    Brendan
    Keymaster

    There are all sorts of functions you can perform on JavaScript Date objects without having to do the math yourself:

    https://www.w3schools.com/js/js_date_methods.asp

    February 22, 2023 at 10:22 AM #48997

    Glen Forister
    Participant

    Great info
    Thanks

Viewing 13 reply threads

You must be logged in to reply to this topic.