Add a number of days to a date script

Hello Everyone,

I am trying to convert a string value to a datetime ?
or how can I add a no of dates on a date format?
Example, I have a date 08/16/2020 I would like to add 5 days to be 08/21/2020 in Scripting block
I have this script, to create all number of eps and create the date assigned to that specific episode
I have the start date value, and I have No of eps value.

I tried to use a moment to add a duration of days, but it’s not working for me
moment.duration().add(1,date)

let importTable = base.getTable(“Titles”);
let importQuery = await importTable.selectRecordsAsync();
let episodesscheduletable = base.getTable(‘Episodes Schedule’);
for (let record of importQuery.records) {
let rowcount =0;
rowcount= record.getCellValue(‘EPS’)
while (rowcount>=0)
{
if (rowcount===0)
{
}else
{
//let now = new Date().toLocaleDateString("");
let date =record.getCellValue(‘Start Date’)
var rightNow = new Date();
//rightNow=date.
let Pattern=record.getCellValue(‘Release Pattern’)
let fullName = record.getCellValue(‘English Title’) + ’ ’ + record.getCellValue(‘Season’) + ’ EP’ + rowcount ;
//if (Pattern===1){ moment.duration().add(1,date);}
//if (Pattern===2){ date=date+6}
//if (Pattern===3){ date=date+5}
//if (Pattern===4){ date=date+4}
//if (Pattern===5){ date=date+3}
//if (Pattern===6){ date=date+2}
//if (Pattern===7){ date=date+1}
//if (Pattern===0){ date=date+rowcount}
await episodesscheduletable.createRecordAsync({
“Name”: fullName,“Release Date”:date
})
}
rowcount=rowcount-1
}
output.text(New record for created!);
}

There are couple things I noticed in your script:

  • Moment.js is a library/extension thing that is not part of standard Javascript. You can’t use Moment unless you import the library.
  • As designed, your script will create a new record for an episode regardless of whether a record for that episode has already been created.
  • Your script starts a loop at 0 and then is explicitly told to do nothing when the index is 0. You could just start at 1.

Try the code below:

const importTable = base.getTable('Titles');
const importQuery = await importTable.selectRecordsAsync();
const episodesScheduleTable = base.getTable('Episodes Schedule');
const episodesScheduleQuery = await episodesScheduleTable.selectRecordsAsync()
const episodesScheduleNames = episodesScheduleQuery.records.map(x => x.name)
const patterns = [
    {pattern: 1, add: 1},
    {pattern: 2, add: 6},
    {pattern: 3, add: 5},
    {pattern: 4, add: 4},
    {pattern: 5, add: 3},
    {pattern: 6, add: 2},
    {pattern: 7, add: 1},
]

for (let record of importQuery.records) {
    var rowCount = record.getCellValue('EPS');
    let current = 1
    
    while (current <= rowCount) {
        var startDate = new Date(record.getCellValue('Start Date'))
        var year = startDate.getFullYear();
        var month = startDate.getMonth();
        var day = startDate.getDate();
        var pattern = record.getCellValueAsString('Release Pattern')
        var add = patterns.filter(x=> Number(x.pattern) == Number(pattern))[0].add
        var fullName = record.getCellValue('English Title') + ' ' + record.getCellValue('Season') + '.' + current
        
        if(!episodesScheduleNames.includes(fullName)) {
            var newDate = new Date(year, month, day + add*current);
            
            await episodesScheduleTable.createRecordAsync({
                "Name": fullName,
                "Release Date": newDate
            })
            output.text(`New record for ${fullName} created!`);
        }
        current++
    }
}

Let me know if that works.