Help

Adding days to a date field when creating a new record

Solved
Jump to Solution
1988 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Jeremy_Lambert
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello everyone,
I am creating a script that will ask the user for a project name and a hand-over date, and then the script will generate a number of records with subsequent dates (to create a Gantt chart, essentially). I want this done through the script because the intervals between the dates is always going to be the same.

I am however struggling to find how to add say 5 days to the date I just collected from the user. I can’t seem to find an easy way to do it when creating a new record. Here is my current code…

output.markdown('Welcome to the Add-New-Project routine');
let projectName = await input.textAsync('What is the name of the new project?');
let handOverDate = new Date();

if (await input.buttonsAsync("Hand-over date", ["Today", "Custom"]) === "Custom") {let newDate = await input.textAsync("Enter a date: M/D[/YYYY] (Current year implied if not included)")
 let year = newDate.slice(-5, -4) === "/" ? "" : "/" + handOverDate.getFullYear().toString()
handOverDate = new Date(`${newDate}${year}`)
     }
output.text(`New project name is , ${projectName} and date ${handOverDate}.`);

// Create all the milestone records
// Make a table from Projects table for linked field Projects
let startUpLeadtime = new Number;
startUpLeadtime = 14;

let table2 = base.getTable("Timeline");
let recordIds = await table2.createRecordsAsync([
{
   fields: {
        "Start date v2": handOverDate,
        "Project": projectName,
        "Milestone": { name: 'Start' },
        "Task start date": handOverDate,
        "Task End date": handOverDate
    },
 },
{
     fields: {
        "Start date v2": handOverDate,
        "Project": projectName,
        "Milestone": { name: 'Start-up' },
        "Task start date": DATEADD(handOverDate,startUpLeadtime,'days'),
        "Task End date": Date(handOverDate+1 'day')
      },
 },
]);

Any ideas? It would be great if I can just add the days from the initial date inside the createRecordsAsync to save some space…

1 Solution

Accepted Solutions
Jeremy_Lambert
5 - Automation Enthusiast
5 - Automation Enthusiast

I found the solution. In short, there isn’t a direct function to do this in javascript. Instead:

  • I obtain the date from a button input (nice little bit of code I re-used),
  • then I have to separate the components of that date into numbers,
  • and recompose the new date from the numbers I obtained. I can add any new values to the current ones at that time.
let handOverDate = new Date();

if (await input.buttonsAsync("Project launch date", ["Today", "Custom"]) === "Custom") {

    let newDate = await input.textAsync("Enter a date: DD/MM[/YYYY] (Current year implied if not included)")
    let newday = newDate.slice(0,2)
    let newmonth = newDate.slice(3,5)
    let year = newDate.slice(-5, -4) === "/" ? "" : "/" + handOverDate.getFullYear().toString()

    handOverDate = new Date(`${newmonth}/${newday}${year}`)

}

var year = handOverDate.getFullYear(); //this returns a number
var month = handOverDate.getMonth(); //this returns a number
var day = handOverDate.getDate(); //this returns a number

let startUpDate = new Date(year, month, day + 1); // startUpDate is one day later than handOverDate
let startUpLeadtime = new Date(year, month, day + 5); // startUpLeadtime is 5 days later than handOverDate

I hope this helps people in the future!

See Solution in Thread

1 Reply 1
Jeremy_Lambert
5 - Automation Enthusiast
5 - Automation Enthusiast

I found the solution. In short, there isn’t a direct function to do this in javascript. Instead:

  • I obtain the date from a button input (nice little bit of code I re-used),
  • then I have to separate the components of that date into numbers,
  • and recompose the new date from the numbers I obtained. I can add any new values to the current ones at that time.
let handOverDate = new Date();

if (await input.buttonsAsync("Project launch date", ["Today", "Custom"]) === "Custom") {

    let newDate = await input.textAsync("Enter a date: DD/MM[/YYYY] (Current year implied if not included)")
    let newday = newDate.slice(0,2)
    let newmonth = newDate.slice(3,5)
    let year = newDate.slice(-5, -4) === "/" ? "" : "/" + handOverDate.getFullYear().toString()

    handOverDate = new Date(`${newmonth}/${newday}${year}`)

}

var year = handOverDate.getFullYear(); //this returns a number
var month = handOverDate.getMonth(); //this returns a number
var day = handOverDate.getDate(); //this returns a number

let startUpDate = new Date(year, month, day + 1); // startUpDate is one day later than handOverDate
let startUpLeadtime = new Date(year, month, day + 5); // startUpLeadtime is 5 days later than handOverDate

I hope this helps people in the future!