Help

Scripting App: Input date with default time(s)

Topic Labels: Scripting extentions
3773 5
cancel
Showing results for 
Search instead for 
Did you mean: 
Nathan_Heironim
6 - Interface Innovator
6 - Interface Innovator

Background:
Hello, any help with this is much appreciated. I’m pretty new to JavaScript, but learning quickly. So far I am proficient with variables, functions, scopes, oh—and commenting things to death!

Problem:
I’m trying to create a script that creates a new event that automatically fills in some recurring contextual information in an [Events] table. However, I cannot find anything anywhere about entering/recording the time of day into a {Date/Time} field when running a script. The event is always at the same time 12pm-1pm (EST in my case) but the date is what needs input(ed) by the user running the script.

So Far:

// identify base and table info
let table = base.getTable('Event List');

// user input "Event Name"
let eventName = await input.textAsync('What\'s the name of this event?');

// Create new record with default event criteria in the [Event List] table
let recordId = await table.createRecordAsync({
'Event Name': eventName,
'Event Type': {
    name: "My Cool Event"
},
'Status': {
    name: "Planning"
},
'Start': new Date(), // This just inputs today's date/time in the "Start" field
'End': new Date(), // This just inputs today's date/time in the "End" field
});

Clarification:
So the focus about my question is on the 'Start' and 'End' fields towards the end of this script. Right now the new Date() call records today’s date/time. But what I’d like to do is ask the user to input the date for the scheduled event—and only the date—during the part in the script where it’s asking for user input (like Event Name). Then the 'Start' and 'End' fields would plug that date along with the default time(s). 12pm start and 1pm end.

Thanks in advance for the help!

5 Replies 5

I ran into a similar problem a while back. While I still have hope that Airtable will add a date input option for scripts, this is what I’m using so far:

let date = new Date();
if (await input.buttonsAsync("Date to use", ["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) === "/" ? "" : "/" + date.getFullYear().toString()
    date = new Date(`${newDate}${year}`)
}

If the user won’t ever use the current date, remove the if wrapper and leave the rest.

Thanks! Glad to know it’s not just me. Sometimes it’s hard to know what’s a feature request and what’s “me being an ignorant newb.”

Nathan_Heironim
6 - Interface Innovator
6 - Interface Innovator

Thanks again @Justin_Barrett for the “Date Picker” module. This plugs in perfectly to the end of the script when I modified it to use these implications.

It looks like this:

'Start': date, // Now inputs the date selected by the user as defined by the 'date' variable...still looking for time
'End': date, // Now inputs the date selected by the user as defined by the 'date' variable...still looking for time
});

Still looking for final piece to the solution:
How would I go about “tabbing” into the ‘Time’ box in a {Date/Time} field? If I’m using the GUI in the table view once you type the ‘Date’ you can simply use the [Tab] key on the QWERTY keyboard to move the cursor into the ‘Time’ box. I’m not sure what script would need added to the updated ‘Start’ and ‘End’ data in this script.

For example: Here are a couple things I’ve tried:

Ex 1:

'Start': date + " 12:00pm", // date works...'+' does not work
'End': date +  " 1:00pm" // date works...'+' does not work
});

Ex 2:

'Start': date && " 12:00pm", // date works...'&&' does not work
'End': date &&  " 1:00pm" // date works...'&&' does not work
});

Ex 3:

'Start': date, " 12:00pm", // date works...additional ',' does not work
'End': date,  " 1:00pm" // date works...additional ',' does not work
});

…none of these above examples work. I’m probably missing some syntax thing. Or maybe there’s a way to define the string as a variable before hand and use that. But either way, I’m still not sure how to get the cursor to land in that ‘Time’ box of the {Time/Date} field to update the time property to the default I’d like it to be for each the ‘Start’ and ‘End’ fields.

Thanks again for helping this ol’ novice.

While the time entry in the field appears to be a separate space, the data stored internally is a single data item. Once you’ve got the date captured, you’ll need to set the time for that item using the appropriate methods on the date object. Here’s a rundown of the set methods on date objects:

https://www.w3schools.com/js/js_date_methods_set.asp

What I recommend is naming the entered date variable something like startTime, then making a copy of it named endTime as follows to :

let endTime = new Date(startTime.getTime());

Now use those set methods on startTime and endTime to set the proper times. Using the example you provided above (12pm to 1pm), it would look like this:

startTime.setHours(12);
endTime.setHours(13); // setHours uses a range of 0-23, so 13 is 1 pm

Once that’s done, you can use them to set the field values, and the times should appear correctly.

'Start': startTime,
'End': endTime

:exploding_head: Thank you, thank you, thank you!!!