Using a Transformer to retrieve and convert a unix date

  • Goal:

Hi, I'm trying to extract the expiry date of a Stripe coupon and use it in JSON to pass to a Send Grid email.

  • Steps:

I'm getting a response from Stripe in 'couponExpiry' that looks something like this (reduced code):

{
  "object": "list",
  "data": [
    {
      "expires_at": 1721047084,
    }
  ]
}

I'm then using a Transformer in an attempt to use the value in 'expires_at' and convert it to a date:

const expiryDate = couponExpiry.data[0].expires_at
return moment.unix(expiryDate).format("DD MMMM YYYY")

I've tried some variations on this but everything I seem to get back from previews is either null or 0 (results in 1st Jan 1970).

is there something obvious I'm doing wrong?

Thanks.

1 Like

I use something like this:

const expiryDate = {{ couponExpiry.data[0].expires_at}}

function formatUnixTimestamp(unixTimestamp) {
    // Convert the Unix timestamp to milliseconds
    var date = new Date(unixTimestamp * 1000);
    
    // Define options for the desired date format
    var options = { day: '2-digit', month: 'long', year: 'numeric' };
    
    // Use toLocaleDateString to format the date
    var formattedDate = date.toLocaleDateString('en-US', options);
    
    return formattedDate;
}

// Convert the expiryDate
const formattedExpiryDate = formatUnixTimestamp(expiryDate);
return formattedExpiryDate;
1 Like

Thanks but I think the problem I'm having is that:

couponExpiry.data[0].expires_at

is not returning anything (why I'm getting 1st Jan 1970).

Does that syntax look correct for returning the value in 'expires_at' from the example response?

Ah, sorry for not catching that was the issue.

If you're using a transformer, then you need to enclose your variable in curly brackets, i.e. {{ couponExpiry.data[0].expires_at }}.

Thanks so much for your responses. Turns out my issue was in more than one place but this helped me debug.

2 Likes