Skip to main content

Automations: Getting Ordinal Values and Months of the Year Through Scripting (Javascript)

  • March 4, 2026
  • 5 replies
  • 54 views

Forum|alt.badge.img

 

Hello everybody I have been working on some AirTable Automation scripts and I figured people might need some help on working with Dates and Times using Javascript on the platform so I am here to share with the community a little guide on both working with AirTable records with Dates along with how to convert them over to JSONs and access/modify them. I specifically cover how to get Ordinal values from the day of the month along with getting the name of the month from the numeric value of the month!

 

Setting Up Your Script

The first thing you will want to do is click the Automation Tab → Create your Trigger → under actions  create an action that says “Run a script”.

Creating The Script Automation

Next you will want to import your data either from a view or a table. In the left pane you will see a category called Inputs. inputs allows you to add in data from previous steps as long as it was mentioned using the output.set() function. 

Inputs Pane

If you have any previous steps with the output.set() function from a previous script you will click the plus sign and for my example Run a script from a previous script and then recordDetails as I have information stored from there using the output.set() I mentioned earlier.

Setting up Use Data from Input Frame

Importing Our Data & Converting to JSON

Next we want to create some helper functions to manipulate our data pulled from our inputs. If your data pulled was a string that is formatted to match a JSON you can use the built in library to parse the string and create a JSON object the allow access to keys which store your values. In this case, I will be using AirTable data converted into a JSON.

// Takes our input and checks if the type is a string, if so we will use the built in JSON library to parse it and return it as a JSON for us.


// input.config() allows us to use the input variables are have stored.
let inputs = input.config();

// we use the variable payload to create a JSON version of our raw_payloads input because it's original format is a string

let payload =
typeof inputs.raw_payload === "string"
? JSON.parse(inputs.raw_payload)
: inputs.raw_payload;

Creating The Helper DateTime Functions

Now that we have our data imported we want to have some functions that will grab and parse our data to exactly how we want it. We have three functions listed below:

  1. localeMonth
    • The input required for this will be a date in the format of (mm/dd/yyyy) and it will pass back the month in a long format. The long format is going to be the full name of the month. 
    • In this example we pass 3/28/2026 and we are given back “March”.
  2. localeDay
    • The input required for this will be a date in the format of (mm/dd/yyyy) and it will pass back the day of the month in a numeric format.
    • In this example we pass 3/28/2026 and we are given back “28”. If you pass 03/02/2026 you will just get “2”.
  3. getOrdinal
    • The input for this will be a date but in the format of the localeDay (“28”). This will give us the proper suffix associated with the day provided.
    • In this scenario have passed “28” into this function and we are returned with “th” as the suffix.
// This will grab the month in long format (January, February, March etc.)
const localeMonth = (d) =>{
return new Date(d).toLocaleString('default', { month: 'long' });
}

// This will grab the day of the month from our date passed in (mm-dd-yyyy)
const localeDay = (d) =>{
return new Date(d).toLocaleString('default', { day: 'numeric' });
}


// This will give us the ordinal value based off of the localeDay function format (st,rd,th)

function getOrdinal(n) {
let s = ["th", "st", "nd", "rd"];
let v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
}


const startMonth = localeMonth(payload["Event Start Date"])
const startSuffix = getOrdinal(localeDay(payload["Event Start Date"]))

const endMonth = localeMonth(payload["Event End Date"])
const endSuffix = getOrdinal(localeDay(payload["Event End Date"]))

Example Inputs & Outputs

Let's apply everything together now and use some sample inputs to showcase what your outputs should look like. For our sample data I will be using this payload to demonstrate. Feel free to just copy this for learning purposes. If you have your own data that you have already cleaned then make sure you have imported it with the steps above.

 

Sample Code:

{ "id": "vnsJ2nfKdlos", "Event Start Date": "2026-03-28", "Event End Date": "2026-04-02"}
Output example and Input

 

Reviewing the console on the right you can see that we have the proper month translated from “3” into March and the date of “28” is now “28th” thanks to our getOrdinal function! Hopefully this helps out when you are cleaning your data and using Javascript to work with Date formats. I have also published a full version of this script to the community as well.

5 replies

coderkid
Forum|alt.badge.img+4
  • Participating Frequently
  • April 1, 2026

@Michael_Tyler , this is a great post, thanks a lot for taking the time to share it... Super helpful, especially for people getting started with scripting in Airtable.

 

That said, one of the cool things about Airtable is how powerful the data layer can be on its own(!) You can actually achieve a lot of this without jumping into automations or scripts.

 

In your case, you could do the same thing directly with formula fields. Here is how...

 

Imagine you have a table where the JSON is stored in a long text field.
 

 

Step 1: Extract the date from the JSON

Since your JSON structure is simple, you can pull out the date with a formula like this:

DATETIME_PARSE(
MID(
{JSON},
FIND('"Event Start Date": "', {JSON}) + LEN('"Event Start Date": "'),
10
),
'YYYY-MM-DD'
)

 

 

Step 2: Format it as Month + ordinal day

Then create another formula field to format the date into something like "March 28th":

DATETIME_FORMAT({Start Date}, 'MMMM') &  " " &DATETIME_FORMAT({Start Date},'Do')

 


You can repeat the same pattern for the end date as well. And your table will look like this:
 

 

Definitely agree scripting gives you more flexibility, but for cases like this it’s nice to keep everything in the base and avoid the extra automation step ;)


ScottWorld
Forum|alt.badge.img+35
  • Genius
  • April 1, 2026

@Michael_Tyler 

FYI: This is already natively built into Airtable’s formula fields, and it’s even simpler than what ​@coderkid posted above.

You just need to use Airtable’s supported DATETIME_FORMAT format specifiers.

So, if you have a date, and you want to get the ordinal number of the date, you just need this formula:

DATETIME_FORMAT({Date},'Do')

That’s it! :) Hope this helps!

- ScottWorld, Expert Airtable Consultant


coderkid
Forum|alt.badge.img+4
  • Participating Frequently
  • April 1, 2026

@ScottWorld thanks, updated!

Correction :
 

DATETIME_FORMAT({Start Date},'MMMM Do')

 

 


ScottWorld
Forum|alt.badge.img+35
  • Genius
  • April 1, 2026

@coderkid 

You’re welcome! :)

- ScottWorld, Expert Airtable Consultant  


DisraeliGears01
Forum|alt.badge.img+21

Ooh, I learned something today, I didn’t realize you could combine datetimeformat specifiers inside a single function. I was just looking at a personal budgeting table I have where I break out Months and Years via formula and was like “Man, it’d be simpler if I could formula Mar 26”. I thought I was going to have to run two DTF functions and concatenate. DATETIME_FORMAT(Date, 'MMM YY') works like a charm.