JWJL
JWJL

Reputation: 15

Adding Days onto a date in a script

I have two fields. One is 'Date Last Checked' the other is 'Date Due'. I need the 'Date due' to automatically be 364 days ahead of the 'Date Last Checked'

Upvotes: 1

Views: 915

Answers (1)

Pavel Shkleinik
Pavel Shkleinik

Reputation: 6347

Basically it comes down to this question: Add days to JavaScript Date

...however it has some App Maker specificity. It seems that the right way to implement it from the Security standpoint will be using onBeforeCreate and onBeforeSave Model Events and Server Side script to enforce data consistency:

// Define this function somewhere in server scripts
function addDays(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}


// onBeforeCreate event handler
record.DueDate = addDays(record.LastChecked, 364);


// onBeforeSave event handler (to keep things simple we can
// update DueDate when any of record's fields is changed)
record.DueDate = addDays(record.LastChecked, 364);

Other benefits of this approach

  • no need to duplicate logic in multiple places on UI
  • updated DueDate will be returned with callback and automatically update UI

Upvotes: 1

Related Questions