Introduction:
In this article, you’ll learn about the various ways you can save dates, times and perform calculations.
Please note: Be sure to use the CORRECT type of field. You should use date, time or date&time custom fields.
Add scripts on node…

Important: DT.setField function cannot be used on the last node for V2 DTs.
1) Save Today’s Date Into a Field
Add this script on the node…
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-CryKbMe7sjSCDPl18jtJI5DR5jtkUWxPXWaLCst6QjH8wxDexfRJic2WRmRXmstr2Y8SxDDWuBO6CQC6IE4KTA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
DT.setField('test_date_1', moment.utc().startOf('day').valueOf());
</script>
2) Save Today’s Date Less 6 Months Into a Field
Add this script on the node…
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-CryKbMe7sjSCDPl18jtJI5DR5jtkUWxPXWaLCst6QjH8wxDexfRJic2WRmRXmstr2Y8SxDDWuBO6CQC6IE4KTA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
DT.setField('test_date_1', moment.utc().startOf('day').subtract(6, 'months').valueOf())
</script>
3) Days between two dates
1. Create a custom field called “daysOld” ( number field, visible: yes )
2. Create a custom field called “DOB”
Note: The Format is important. It should be MM/DD/YYYY and of input type: combobox.

3. Copy/paste this code into any script sections of a node that captures a date in the past. For example a question that asks “What is your date of birth?”
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-CryKbMe7sjSCDPl18jtJI5DR5jtkUWxPXWaLCst6QjH8wxDexfRJic2WRmRXmstr2Y8SxDDWuBO6CQC6IE4KTA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
//Get our 2 dates
today = new Date()
past = new Date('{DOB}')
console.log('DOB: '+past);
//Calculate the time difference of two dates
var diff = today.getTime() - past.getTime();
var days = Math.floor(diff / (1000 * 3600 * 24));
//Print result on browser console
console.log("Number of days old: "+days);
//Assign result to custom field
DT.setField('daysOld', days);
</script>
4. Publish and access via direct link. Check the browser console for the number of days to print.
Decision Node: You can also use decision nodes to compare 2 dates…

4) Time between two dates in days, months, and years
- You will need to create the DOB custom field as described previously
- Create three number custom fields called “days”, “months”, “years”
- Copy/paste this code into any script sections of a node that captures a date in the past. For example a question that asks “What is your date of birth?”.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-CryKbMe7sjSCDPl18jtJI5DR5jtkUWxPXWaLCst6QjH8wxDexfRJic2WRmRXmstr2Y8SxDDWuBO6CQC6IE4KTA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
//Get our 2 dates
today = new Date()
past = new Date('{DOB}')
console.log('DOB: '+past);
dy = today.getYear() - past.getYear();
dm = today.getMonth() - past.getMonth();
dd = today.getDate() - past.getDate();
if (dd < 0) { dm -= 1; dd += 30; }
if (dm < 0) { dy -= 1; dm += 12; }
console.log(dy+" Year(s), "+dm+" Month(s), and "+dd+" Days.");
//Assign result to custom field
DT.setField('days', dd);
DT.setField('months', dm);
DT.setField('years', dy);
</script>
5) Get age from a given date of birth
- You will need to create a custom field called {birth_date}
- You will need to create a custom field called {age}
Where you capture the birth_date, you will need to fire this script on exit of the node:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-CryKbMe7sjSCDPl18jtJI5DR5jtkUWxPXWaLCst6QjH8wxDexfRJic2WRmRXmstr2Y8SxDDWuBO6CQC6IE4KTA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
var today = new Date();
var birthDate = new Date('{birth_date}');
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
console.log(age);
DT.setField('age', age);
</script>
Then you can access the correct age using the {age} custom field in your Decision Tree.
6) Assign MM-DD-YY to their own individual custom fields.
- Create 3 custom fields which will be used to store the value of MM, DD, and YY
- Create a decision node to set the value of those 3 fields.
- Apply Field Filters to get individual values of MM, DD, and YY
Example of how it would look like inside the decision node.
day_customfield > {yourdate|date:'DD'}
month_customfield > {yourdate|date:'MM'}
year_customfield > {yourdate|date:'YYYY'}

7.) Adding and Subtracting Years, Months or Days from today’s date.
1. Create a DATE type custom field first.
2. Use one of the scripts below.
A.) test_date_1 will subtract 308 weeks from today.
B.) test_date_2 will subtract 6 years from today.
C.) test_date_3 will add 14 days from today.
You will need to edit the script and only use one of them depending on what you want to add or subtract.
DT.setField(‘YOUR_FIELD_HERE‘, moment.utc().startOf(‘day’).ADD/SUBTRACT(NUMBERVALUE, ‘WEEKS/DAYS/YEARS‘).valueOf())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-CryKbMe7sjSCDPl18jtJI5DR5jtkUWxPXWaLCst6QjH8wxDexfRJic2WRmRXmstr2Y8SxDDWuBO6CQC6IE4KTA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
DT.setField('test_date_1', moment.utc().startOf('day').subtract(308, 'weeks').valueOf())
DT.setField('test_date_2', moment.utc().startOf('day').subtract(6, 'years').valueOf())
DT.setField('test_date_3', moment.utc().startOf('day').add(14, 'day').valueOf())
</script>