Copy link to clipboard
Copied
Have attempted to generate the number of days between two dates using a custom calculation in a text field form and no matter what formula I use the field blank.
Can anyone help?
I assume you have two dates fields and want to show number of days in 3rd field, let's say two dates fields are named "Date1" and "Date2", use this as custom calculation script of 3rd field:
var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;
if (date1 && date2) {
var d1 = new Date(date1);
var d2 = new Date(date2);
var diffInMs = d2.getTime() - d1.getTime();
var diffInDays = diffInMs / (1000 * 60 * 60 * 24);
event.value = diffInDays.toFix
...
Here you go:
var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;
if (date1 && date2) {
var d1 = new Date(date1);
var d2 = new Date(date2);
var year1 = d1.getFullYear();
var year2 = d2.getFullYear();
var month1 = d1.getMonth();
var month2 = d2.getMonth();
var diffInMonths = (year2 - year1) * 12 + (month2 - month1);
event.value = diffInMonths;}
else {
event.value = "";}
Copy link to clipboard
Copied
I assume you have two dates fields and want to show number of days in 3rd field, let's say two dates fields are named "Date1" and "Date2", use this as custom calculation script of 3rd field:
var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;
if (date1 && date2) {
var d1 = new Date(date1);
var d2 = new Date(date2);
var diffInMs = d2.getTime() - d1.getTime();
var diffInDays = diffInMs / (1000 * 60 * 60 * 24);
event.value = diffInDays.toFixed(0);}
else {
event.value = "";}
Copy link to clipboard
Copied
Perfect it works!
Is there a Java script to count months between two dates.
Copy link to clipboard
Copied
Here you go:
var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;
if (date1 && date2) {
var d1 = new Date(date1);
var d2 = new Date(date2);
var year1 = d1.getFullYear();
var year2 = d2.getFullYear();
var month1 = d1.getMonth();
var month2 = d2.getMonth();
var diffInMonths = (year2 - year1) * 12 + (month2 - month1);
event.value = diffInMonths;}
else {
event.value = "";}
Copy link to clipboard
Copied
Thank you so much!