Gallery
Kuovonne's Guide to Scripting in Airtable
Share
Explore
Free Scripts

Formatted Timestamp

Your donations tell Kuovonne that you value her content and want her to continue publishing.

Inspiration

A user on the Airtable community forums that he wanted to include a nicely formatted date in a weekly email. Although he could get the expected trigger time for the automation, the result was an ugly ISO string.

Discussion

To get the current date and time in JavaScript, create a new date object.
const now = new Date()
However this date object will not be formatted for humans to read.
Fortunately, you can format date objects for human consumption with toLocaleString().
In order to format the timestamp, you need to specify several options
timezone. You can see a list of here.
date style. If you want to include the date, you can choose from the full, long, medium, or short format.
time style. If you want to include the time, you can choose from the full, long, medium, or short format.
These options will give a short date and time in New York timezone, such as 4/26/23, 6:53 PM.
const options = {
timeZone: "America/New_York",
dateStyle: "short",
timeStyle: "short",
}
If you want only the date without the time, leave out the timeStyle.
const options = {
timeZone: "America/New_York",
dateStyle: "short",
}
You also need the locale. The locale indicates the order of elements in the date (MM/DD/YYYY versus DD/MM/YYYY) and the language. I usually use en-us because I am located in the United States. If you are located elsewhere, you can see a list of locales
.
const locale = "en-us"
Now that you have your locale and options, you can get the formatted date/time.
const formattedDateTime = now.toLocaleString(locale, options)
Finally, have the script output the formatted date/time for use in a future automation step.
output.set("automationTimeFormatted", formattedDateTime)
Test the script to see the results.
image.png
Then use the results in a future automation action.
image.png
Here is the full script
const now = new Date()
const locale = "en-us"
const options = {
timeZone: "America/New_York",
dateStyle: "short",
timeStyle: "short",
}
const formattedDateTime = now.toLocaleString(locale, options)
output.set("automationTimeFormatted", formattedDateTime)
The content in this guide is free, but creating it takes time and money. If you like this content, .

Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.