A practical field guide from Automation Ace.
How to Parse a Timestamp into Parts Using JavaScript in a Zapier Code Step
Zapier's Formatter by Zapier can reformat dates, but if you need to split a timestamp into multiple discrete parts — the year alone in one field, the month in another, the weekday name in a third — a JavaScript Code step gives you precise control. This is useful when downstream steps require individual date components: creating a file named with the year and month, routing logic based on the day of week, storing the hour for analytics, or building human-readable date strings in a specific format. This guide covers the patterns for extracting every useful part from a timestamp in a Zapier JavaScript Code step.
Basic Timestamp Parsing
JavaScript's Date object parses ISO 8601 strings, Unix timestamps, and most common date formats automatically:
const ts = inputData.timestamp; // e.g. "2025-03-15T14:30:00Z" or "2025-03-15 14:30:00"
const d = new Date(ts);
// UTC parts
const year = d.getUTCFullYear(); // 2025
const month = d.getUTCMonth() + 1; // 1–12 (getUTCMonth is 0-indexed)
const day = d.getUTCDate(); // 1–31
const hour = d.getUTCHours(); // 0–23
const minute = d.getUTCMinutes(); // 0–59
const second = d.getUTCSeconds(); // 0–59
const dayOfWeek = d.getUTCDay(); // 0=Sunday, 6=Saturday
const dayNames = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
const monthNames = ['January','February','March','April','May','June',
'July','August','September','October','November','December'];
output = {
year: String(year),
month: String(month).padStart(2, '0'),
day: String(day).padStart(2, '0'),
hour: String(hour).padStart(2, '0'),
minute: String(minute).padStart(2, '0'),
second: String(second).padStart(2, '0'),
weekday_name: dayNames[dayOfWeek],
month_name: monthNames[month - 1],
date_only: `${year}-${String(month).padStart(2,'0')}-${String(day).padStart(2,'0')}`,
time_only: `${String(hour).padStart(2,'0')}:${String(minute).padStart(2,'0')}`,
unix_timestamp: String(Math.floor(d.getTime() / 1000))
};
Always use the UTC variants (getUTCFullYear, getUTCMonth, etc.) when the timestamp includes timezone information — otherwise JavaScript applies the server's local timezone, which produces unexpected results in Zapier's cloud environment.
Parsing with a Specific Timezone
If you need parts in a specific timezone rather than UTC, use Intl.DateTimeFormat with the timeZone option — available in Zapier's JavaScript Code step environment:
const ts = inputData.timestamp;
const tz = inputData.timezone || 'America/New_York'; // e.g. "America/Chicago", "Europe/London"
const d = new Date(ts);
const fmt = (part) => new Intl.DateTimeFormat('en-US', {
timeZone: tz,
[part]: part === 'hour' ? 'numeric' : '2-digit',
hour12: false
}).format(d);
const parts = new Intl.DateTimeFormat('en-US', {
timeZone: tz,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
weekday: 'long',
hour12: false
}).formatToParts(d);
const get = (type) => parts.find(p => p.type === type)?.value || '';
output = {
year: get('year'),
month: get('month'),
day: get('day'),
hour: get('hour') === '24' ? '00' : get('hour'), // midnight edge case
minute: get('minute'),
second: get('second'),
weekday_name: get('weekday'),
date_only: `${get('year')}-${get('month')}-${get('day')}`,
time_only: `${get('hour') === '24' ? '00' : get('hour')}:${get('minute')}`,
timezone: tz
};
Pass timezone as an input data field mapped from a previous step — for example, the user's timezone from a CRM field, or a static timezone for your team. Valid timezone identifiers follow the IANA tz database format (America/New_York, Europe/Berlin, Asia/Tokyo, etc.).
Handling Unix Timestamps as Input
If the input is a Unix timestamp (integer seconds since epoch) rather than a date string:
const unixTs = parseInt(inputData.unix_timestamp, 10);
const d = new Date(unixTs * 1000); // JavaScript Date uses milliseconds
const year = d.getUTCFullYear();
const month = d.getUTCMonth() + 1;
const day = d.getUTCDate();
const hour = d.getUTCHours();
const minute = d.getUTCMinutes();
output = {
year: String(year),
month: String(month).padStart(2, '0'),
day: String(day).padStart(2, '0'),
hour: String(hour).padStart(2, '0'),
minute: String(minute).padStart(2, '0'),
date_only: `${year}-${String(month).padStart(2,'0')}-${String(day).padStart(2,'0')}`,
iso_string: d.toISOString()
};
Practical Output Patterns
Common formatted outputs built from extracted parts:
// File name: "Report-2025-03.pdf"
const fileName = `Report-${year}-${String(month).padStart(2,'0')}.pdf`;
// Human-readable: "Saturday, March 15, 2025 at 2:30 PM"
const ampm = hour >= 12 ? 'PM' : 'AM';
const hour12 = hour % 12 || 12;
const readable = `${dayNames[dayOfWeek]}, ${monthNames[month-1]} ${day}, ${year} at ${hour12}:${String(minute).padStart(2,'0')} ${ampm}`;
// Quarter: "Q1", "Q2", "Q3", "Q4"
const quarter = `Q${Math.ceil(month / 3)}`;
When to Use Code Step vs. Formatter for Date Parsing
Use Formatter by Zapier date modifiers for single-field reformatting — converting a date to a different format in one downstream field. Use a JavaScript Code step when you need multiple parts as separate output fields (year, month, day each as their own field for use in different downstream steps), timezone conversion with non-obvious IANA identifiers, or computed outputs like quarter, fiscal year, or weekday-based routing logic.
The two patterns to remember: use UTC getters (getUTCFullYear, etc.) when your input timestamp has timezone info; useIntl.DateTimeFormat.formatToParts()when you need parts in a specific named timezone.padStart(2, '0')is your friend for keeping month and day zero-padded. And always output every part as a string — Zapier Code step output values must be strings or numbers, not mixed types.
For the last day of a month calculation, see the dedicated Code step guide. For JavaScript libraries available in Code steps, see JavaScript libraries in Zapier Code steps. For help with a date-driven workflow, talk to Automation Ace.
Disclaimer: This article may include links to apps, products, or services. Some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.