Simple functions: Change to ISO Date / Change to Coda Date - Steal them for your Packs!

Hey everyone, I wanted to share a couple ‘global’ date conversion functions I’ve been using in most of my Packs. The context of why I created them is because most APIs give and receive ISO dates, but working with Coda dates are so much easier in the doc. With these two functions, I can either convert an ISO date to a Coda date or vice versa, just by wrapping an item with either function.

First the functions:
You can simply copy and paste these into your code (even outside of formulas or sync tables)

//global Coda date to ISO Date helper
function toIsoDate(convDate: Date): String {
  let isoDate = convDate.toISOString();
  return isoDate;
};

//global ISO Date to Coda date helper
function toCodaDate(isoDateToConv: String): String {
  let date = isoDateToConv.toString();
  let codaDate = new Date(date).toLocaleDateString('us');
  return codaDate;
};

The first function converts a Coda date that is passed into a formula into an ISO date. The second does the reverse.

How I use it:
This is an example from a bit of code from the Hootsuite Pack:

 messageResult.push({
      scheduledSendTime: toCodaDate(messageObj.scheduledSendTime),
      publishedDate: toCodaDate(messageObj.scheduledSendTime),
      messageText: messageObj.text,
      messageId: messageObj.id,
      postUrl: messageObj.postUrl,
    });

Limitation:
Unfortunately, these functions have one significant limitation, namely that it won’t help convert times to local timezones. To do this, you’ll need to work a bit more magic in your formula with the context parameter.

Feedback:
I’d love your feedback on how I can improve these functions! I’m still a novice JavaScripter, so I’m sure there’s much I can still learn. I wanted to share though, because I thought someone else could get a little use out of them. Looking forward to your thoughts!

8 Likes