Post

3 followers Follow
0
Avatar

Calculation field duration two dates

Looking to try to create a calculation field for days vacant for rental properties.  I have a move out date and move in date field- I want it to calculate even if there is not a "move in date" put in.  For example, if someone moves out on 11/20/18, I want it to calculate the days vacant based on today's date UNLESS a move in date is put in.  I have this so far, but I'm stuck.

var start = moment(@Move Out Date);
var end = moment(@Move In Date);
var diffMS = end.diff(start) // difference in milliseconds;
var diffDur = moment.duration(diffMS);
var diffDays = diffDur.asDays();
diffDays

Jon Smith

Please sign in to leave a comment.

2 comments

0
Avatar

To calculate from the day of the move until today, try this:

Field Name: Days Vacant (calculation field)

Math.floor((Date.now() - @Move out date) / 86400000)

Unit: Days

 

To calculate how much days was the the  properties ocuped (with move in and move out date fields) 

Math.floor((@Move Out - @Move In) / 86400000) + 1

Unit: Days

Sergio Pérez Jaramillo 0 votes
0
Avatar

That will return a correct result for the day you create the calculation or for the day you edit the field Move out date.
But you will see the same number tomorrow, next week etc. - cause date.now() is not dynamical. You need a workaround which I've described multiple times in this forum. 
For more info see here: https://help.podio.com/hc/en-us/community/posts/115000113951-Trying-return-a-number-on-subtracting-today-s-date-from-a-given-date-in-a-field-?page=1#community_comment_115000105451

After you've implemented the workaround you can calculate the days difference like this:

var start = moment(@Move Out Date);
var in = moment(@Move In Date);
var today = moment(@max of today's date); // = the date field from the related extra app
var end = in != null ? in : today;
end.diff(start,"d") // difference in days;

If you want to include the start day in the diff add +1.

end.diff(start,"d") + 1 

Rainer 

Rainer Grabowski 0 votes