# JavaScript examples: dates and times

> Run Custom Code snippets for due dates, business days, months, quarters, ages, week numbers and business hours, in plain JavaScript.

Source: https://tadabase.com/docs/workflows/dates-times-snippets  
Status: Verified Nov 17, 2026. Checked against the current product

Snippets for a **Run Custom Code** step that work with dates: days until a due date, adding days or business days, month boundaries, quarters, ages, week numbers and business hours. For how the step works, see [Run custom JavaScript in a workflow](https://tadabase.com/docs/workflows/running-custom-javascript).

## Before you start

1. **Add the step** Add a **Run Custom Code** step to your workflow and leave **Language** set to **JavaScript**.
2. **Add the inputs** Under **Data to use in the code**, click **+ Add Input** once for each input the snippet lists. Type the name from the snippet in **Variable name** and choose where the value comes from, for example **Record Value (Source)** or **Custom Value**.
3. **Paste and test** Click the **Code** box to open the code editor, paste the snippet, and click **Run**. The **Response** tab shows what the code returned.
4. **Use the results** In a later step, open the value picker, choose **Step Data**, pick this step, and pick the key the snippet returned (for example `full_name`).

An input is available in the code as `$` followed by its variable name: an input named `first_name` is `$first_name`. Add every input a snippet uses; a name with no matching input stops the code with a "not defined" error.

> **Plain JavaScript only:** The code runs as plain JavaScript (ES2020 or later). There are no `Str`, `Num`, `Arr` or `DateTime` helper objects, so older examples that used them stop with an error. The step adds `returnData`, `fail`, `getLoggedInUser`, `getAppDetails`, `getAppVariable` and `getAppSecret`; see [Run custom JavaScript in a workflow](https://tadabase.com/docs/workflows/running-custom-javascript).

## Today, and your app’s time zone

The code runs on a separate code server, so `new Date()` gives that server’s clock, which may not match your app’s time zone. To work with today’s date as your app sees it, pass it in as an input:

| Input type | What the code receives | Example |
| --- | --- | --- |
| **Current Date** | Today in the app’s time zone | `2026-04-26` |
| **Current Time** | The time now in the app’s time zone | `14:30:00` |
| **Current Date & Time** | Both together | `2026-04-26 14:30:00` |

`getAppDetails().timezone` returns the name of the app’s time zone if you need to show it.

The snippets below expect dates in the form `YYYY-MM-DD`. Before you rely on a date field from a record, click **Run** in the code editor with a sample record and check, in the **Sample Data** tab, how that field’s value arrives. If it is in another form, change `toDate` to read it.

## Helpers for this page

Paste these at the top of your code. They treat every date as a calendar day at midnight UTC, so daylight-saving changes never shift a result by a day. Each snippet lists the helpers it uses.

```js
// "2026-04-26" or "2026-04-26 14:30:00" -> a date at midnight UTC (null if not a date)
function toDate(text) {
  const m = String(text ?? "").match(/^(\d{4})-(\d{2})-(\d{2})/);
  return m ? new Date(Date.UTC(+m[1], +m[2] - 1, +m[3])) : null;
}
const ymd = d => d.toISOString().slice(0, 10);          // date -> "2026-04-26"
const addDays = (d, n) => new Date(d.getTime() + n * 86400000);
const daysBetween = (from, to) => Math.round((to - from) / 86400000);
const isWeekend = d => d.getUTCDay() === 0 || d.getUTCDay() === 6;
```

## Show a date as "April 26, 2026"

```js
// Input: date   "2026-04-26" -> "April 26, 2026"
// Uses: toDate
const MONTHS = ["January", "February", "March", "April", "May", "June", "July",
                "August", "September", "October", "November", "December"];
const d = toDate($date);

returnData("friendly", d ? `${MONTHS[d.getUTCMonth()]} ${d.getUTCDate()}, ${d.getUTCFullYear()}` : "");
```

## Days until a due date

A negative number means the date has passed.

```js
// Inputs: due_date, today (Current Date)
// Uses: toDate, daysBetween
const due = toDate($due_date);
const today = toDate($today);
const days = due && today ? daysBetween(today, due) : null;

returnData("days_until", days);
returnData("is_overdue", days !== null && days < 0);
returnData("due_status", days === null ? "no_date" : days < 0 ? "overdue" : days <= 3 ? "soon" : "on_track");
```

## Add days to a date

```js
// Inputs: start_date, days (Custom Value, for example 30)
// Uses: toDate, ymd, addDays
const start = toDate($start_date);

returnData("end_date", start ? ymd(addDays(start, Number($days) || 30)) : "");
```

## First and last day of this month

```js
// Input: today (Current Date)
// Uses: toDate, ymd
const d = toDate($today) || fail("today is not in YYYY-MM-DD form");
const first = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), 1));
const last = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + 1, 0));

returnData("month_start", ymd(first));
returnData("month_end", ymd(last));
```

## Next business day

Skips Saturdays and Sundays. It does not know your holidays; keep a list of holiday dates and skip those too if you need to.

```js
// Input: today (Current Date)
// Uses: toDate, ymd, addDays, isWeekend
let d = addDays(toDate($today) || fail("today is not in YYYY-MM-DD form"), 1);
while (isWeekend(d)) d = addDays(d, 1);

returnData("next_business_day", ymd(d));
```

## Add business days

```js
// Inputs: start_date, business_days (Custom Value)
// Uses: toDate, ymd, addDays, isWeekend
let d = toDate($start_date);
let remaining = parseInt($business_days, 10) || 0;

while (d && remaining > 0) {
  d = addDays(d, 1);
  if (!isWeekend(d)) remaining--;
}

returnData("delivery_date", d ? ymd(d) : "");
```

## Which quarter a date is in

```js
// Input: date   "2026-04-26" -> "Q2 2026"
// Uses: toDate
const d = toDate($date);

returnData("quarter", d ? `Q${Math.floor(d.getUTCMonth() / 3) + 1} ${d.getUTCFullYear()}` : "");
```

## Age in whole years

```js
// Inputs: birth_date, today (Current Date)
// Uses: toDate
const birth = toDate($birth_date);
const today = toDate($today);
let age = null;

if (birth && today) {
  age = today.getUTCFullYear() - birth.getUTCFullYear();
  const beforeBirthday = today.getUTCMonth() < birth.getUTCMonth() ||
    (today.getUTCMonth() === birth.getUTCMonth() && today.getUTCDate() < birth.getUTCDate());
  if (beforeBirthday) age--;
}

returnData("age", age);
returnData("is_adult", age !== null && age >= 18);
```

## Name of the day of the week

```js
// Input: date
// Uses: toDate, isWeekend
const DAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const d = toDate($date);

returnData("day_name", d ? DAYS[d.getUTCDay()] : "");
returnData("is_weekend", d ? isWeekend(d) : false);
```

## ISO week number

```js
// Input: date   ISO 8601 week number (weeks start on Monday)
// Uses: toDate
const d = toDate($date) || fail("date is not in YYYY-MM-DD form");
const t = new Date(d.getTime());
t.setUTCDate(t.getUTCDate() + 4 - (t.getUTCDay() || 7));   // the Thursday of this week
const yearStart = new Date(Date.UTC(t.getUTCFullYear(), 0, 1));

returnData("iso_week", Math.ceil(((t - yearStart) / 86400000 + 1) / 7));
returnData("iso_year", t.getUTCFullYear());
```

## Good morning, afternoon or evening

```js
// Input: now_time (Current Time, for example "14:05:00")
const hour = parseInt(String($now_time ?? "").slice(0, 2), 10);

let greeting = "Hello";
if (hour < 12) greeting = "Good morning";
else if (hour < 18) greeting = "Good afternoon";
else if (hour >= 18) greeting = "Good evening";

returnData("greeting", greeting);
```

## Inside business hours right now?

```js
// Inputs: today (Current Date), now_time (Current Time)
// Monday to Friday, 9:00 to 17:00 in the app's time zone
// Uses: toDate, isWeekend
const hour = parseInt(String($now_time ?? "").slice(0, 2), 10);
const today = toDate($today) || fail("today is not in YYYY-MM-DD form");
const open = !isWeekend(today) && hour >= 9 && hour < 17;

returnData("is_business_hours", open);
```

## End time from a start time and a duration

The time is read and written as a plain clock time, with no time-zone conversion.

```js
// Inputs: start ("2026-04-26 14:30:00", for example from Current Date & Time), minutes
const m = String($start ?? "").match(/^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})/);
const start = m ? new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5])) : null;
const end = start ? new Date(start.getTime() + (Number($minutes) || 60) * 60000) : null;

returnData("end_time", end ? end.toISOString().slice(0, 16).replace("T", " ") : "");
```
