How to Convert Date Format from "Sunday, August 13th, 2023 at 14:28" to "HH:mm D/M/YYYY" in Coding?

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

i get this text in an email just as shown below “Date: On Sunday, August 13th, 2023 at 14:28” and i would like to use that in next step as format HH:mm D/M/YYYY instead. how do i code that?

------- Forwarded Message -------
From: mike@hsbc.com mike@hsbc.com
Date: On Sunday, August 13th, 2023 at 14:28
Subject: Order 2742 was proccesed.
To: team@protonmail.com team@protonmail.com

To extract the date and time from the email text and format it as “HH:mm D/M/YYYY”, you can use the following code in a Pipedream code step: import { parse, format } from "date-fns"; export default defineComponent({ async run({ steps }) { const emailText = steps.trigger.event.text; // Replace with the actual email text variable const regex = /Date: On (.*? at \d{2}:\d{2})/; const match = emailText.match(regex); if (match) { const dateString = match[1]; const parsedDate = parse(dateString, "EEEE, MMMM do, yyyy 'at' HH:mm", new Date()); const formattedDate = format(parsedDate, "HH:mm d/M/yyyy"); return formattedDate; } else { console.log("Date not found in email text"); } }, }); This code uses the date-fns library to parse and format the date. It first extracts the date string from the email text using a regular expression, then parses it into a JavaScript Date object, and finally formats it as “HH:mm D/M/YYYY”. The formatted date will be returned from the step and can be used in the next step of your workflow.

Wait, are you developing for HSBC? That must be awesome!