What is the Best Way to Format a Date Time String for Use in NodeJS or Google Sheets?

This topic was automatically generated from Slack. You can find the original thread here.

another one that should be pretty easy. One of the data outputs is a date time string like this “2023-05-31T10:55:41Z”
What is the best way to format that string into a usable date time value? Is there a method in nodeJS or is it better to do it in the Google Sheet that the data is pushed to?

you can use Pipedream Formatting > [Date/Time] Format action

where would I put that step? I have a Zoom participation Object array step followed by a NodeJS step that formats that data into an array of arrays.

if you want to format the date in each item in an array then it’s best to use the code. You can ask @U05A7KLNALC for it. For example

@U05A7KLNALC, I have an array with structure [ {"isodate": "2023-05-31T10:55:41Z"}, {"isodate": "2023-05-31T10:55:41Z"}], how can I format the isodate field to the date format MMMM DD YYYY HH:mm:ss using Nodejs

You can use the date-fns library to format the date in your array. First, you’ll need to import the parseISO and format functions from date-fns. Then, you can map through the array and format the isodate field. Here’s an example: import { parseISO, format } from 'date-fns'; const inputArray = [ { "isodate": "2023-05-31T10:55:41Z" }, { "isodate": "2023-05-31T10:55:41Z" } ]; const formattedArray = inputArray.map(item => { const parsedDate = parseISO(item.isodate); const formattedDate = format(parsedDate, "MMMM dd yyyy HH:mm:ss"); return { ...item, formattedDate }; }); console.log(formattedArray); This code will output an array with the formatted date: ```
on [ { “isodate”: “2023-05-31T10:55:41Z”, “formattedDate”: “May 31 2023 10:55:41” }, { “isodate”: “2023-05-31T10:55:41Z”, “formattedDate”: “May 31 2023 10:55:41” } ]

write the code in a Pipedream Node.js action

Here’s the code to format the date in a Pipedream Node.js action: ```
import { parseISO, format } from ‘date-fns’; export default defineComponent({ props: { isodate: { type: “string”, label: “ISO Date String”, description: “The ISO date string to be formatted”, }, dateFormat: { type: ‘string’, label: “Date Format”, description: “The desired date format, e.g. ‘MMMM dd yyyy HH:mm:ss’”, } }, async run({ steps, $ }) { const parsedDate = parseISO(this.isodate); const formattedDate = format(parsedDate, this.dateFormat); return formattedDate; }, });

thats cool

, I have an array in params steps.list_participants with structure [ {"isodate": "2023-05-31T10:55:41Z"}, {"isodate": "2023-05-31T10:55:41Z"}], how can I format the isodate field to the date format MMMM DD YYYY HH:mm:ss using Pipedream Nodejs code action

You can format the isodate field in the array using the date-fns library. Here’s a Pipedream Node.js code action to achieve this: import { parseISO, format } from 'date-fns'; export default defineComponent({ props: { participants: { type: "string[]", label: "Participants", description: "Array of participants with isodate field", }, }, async run({ $ }) { const formattedParticipants = this.participants.map(participantJson => { const participant = JSON.parse(participantJson); const parsedDate = parseISO(participant.isodate); const formattedDate = format(parsedDate, "MMMM dd yyyy HH:mm:ss"); return { ...participant, formattedDate }; }); return formattedParticipants; }, }); Add this action to your workflow after the Zoom participation Object array step. It will take the array of participants as input and return an array with the formatted isodate field.

feel free to play with Pi until it provide you the code that fits your need.

Also if you’re on advanced/business plan, you can make use of Pipedream AI Code generation directly from Pipedream UI

Yep, now that I know the general workflow will work - I’m going to upgrade