// Function to calculate the difference between two dates in days
function calculateDateDifference(startDate, endDate) {
if (typeof moment === 'undefined') {
throw new Error('moment.js library is not loaded');
}
const start = moment(startDate).startOf('day');
const end = moment(endDate).startOf('day');
const duration = end.diff(start, 'days');
return duration;
}
// Example usage:
var fromDate = field('From Date'); // Replace 'From Date' with your actual field name
var toDate = field('To Date'); // Replace 'To Date' with your actual field name
var dateDifference = ''; // Initialize dateDifference as an empty string
if (fromDate && toDate) {
dateDifference = calculateDateDifference(fromDate, toDate);
field('Days', dateDifference); // Replace 'Days Staying' with your actual field name
}
// Output the result
dateDifference ? dateDifference : 'Date difference not calculated';