I’m working on a Zapier automation and need to break down a datetime string into separate components. I want to extract just the year, month, and day parts and ignore the time portion.
My input looks like this:
timestamp: 2020-05-15 14:22:45
Here’s the code I wrote:
let { timestamp } = inputData;
let { datepart } = timestamp.split(' ')[0];
const parts = datepart.split("-");
const yr = parts[0];
const mn = parts[1];
const dy = parts[2];
output = [{yr, mn, dy}];
But I keep getting this error message:
“We had trouble sending your test through. TypeError: Cannot read property ‘split’ of undefined”
What am I doing wrong here? The datetime string format should be correct but something isn’t working as expected.
hey, your destructuring is wrong. change let { datepart } = timestamp.split(' ')[0]; to let datepart = timestamp.split(' ')[0];. drop the curly braces - you’re just dealing with a string.
Yeah, the destructuring issue’s covered above, but there’s another problem - you’re not checking if the timestamp actually exists in inputData. I hit this exact error working with webhook data in Zapier. Sometimes the input field comes through as null or undefined, especially during testing. Add a simple check first:
let { timestamp } = inputData;
if (!timestamp) {
return [{yr: '', mn: '', dy: ''}];
}
let datepart = timestamp.split(' ')[0];
const parts = datepart.split("-");
const yr = parts[0];
const mn = parts[1];
const dy = parts[2];
output = [{yr, mn, dy}];
This saved me hours of debugging when my Zapier workflows would randomly fail on empty data.
Your destructuring syntax on line 2 is wrong. You’re trying to destructure a string like it’s an object. Just assign the result directly to a variable - no curly braces needed. Also, add some error handling since Zapier sometimes sends weird data formats. Here’s the fix I’ve used in my own Zapier workflows:
let { timestamp } = inputData;
let datepart = timestamp.split(' ')[0];
const parts = datepart.split("-");
const yr = parts[0];
const mn = parts[1];
const dy = parts[2];
output = [{yr, mn, dy}];
This’ll fix your TypeError and extract the date components properly.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.