Nov 18, 2021 09:40 AM
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…
Solved! Go to Solution.
Nov 19, 2021 08:29 AM
I found the solution. In short, there isn’t a direct function to do this in javascript. Instead:
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!
Nov 19, 2021 08:29 AM
I found the solution. In short, there isn’t a direct function to do this in javascript. Instead:
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!