How to capture dates?
# 🤝help
h
How to format datetime? I want to return it in a message as "Monday 31 July 2023" but instead I'm getting "2023-07-31T00:00:00.000Z"
Any programmers here who have an idea how to output dates in a formatted manner! TIA
m
hint: just ask chatGPT. It came up with this right here, try if it works: const dateString = "2023-07-31T00:00:00.000Z"; const date = new Date(dateString); const options = { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }; const formattedDate = date.toLocaleDateString('en-US', options); console.log(formattedDate); // Output: Monday, July 31, 2023 if it doesn't, ask chatGPT why. AI can help you with literally anything. 🙂
h
Thanks, that looks impressive. Umm, now where does it go? Slowly finding that this no code chatbot isn't really designed for beginners scratches head
@melodic-pharmacist-33482 @acceptable-kangaroo-64719 Guys, can you help out a Botpress newbie, non programmer here, looking at the above code like its something from an alien planet. Not sure what to do with it. TIA
a
Here's a step-by-step for you: 1. Make the flow from the picture. 2. Ensure the capture card in "get_the_date" is of the "datetime" type. It's at the very bottom of the list. 3. Store the result in a new variable called "userDate" 4. Add an "Execute code" card with the following code:
Copy code
js
const date = new Date(workflow.userDate)
const options = {
  weekday: 'long',
  day: 'numeric',
  month: 'long',
  year: 'numeric'
}

const formattedDate = date.toLocaleDateString('en-us', options)
workflow.date = formattedDate
5. After the execute code card, add a text card with
@date
written in it. Test it out and let me know if it works 🙌

https://cdn.discordapp.com/attachments/1130017484725047318/1131541225963524227/image.png

https://cdn.discordapp.com/attachments/1130017484725047318/1131541226269712434/image.png

https://cdn.discordapp.com/attachments/1130017484725047318/1131541226630418503/image.png

h
Thanks for the info above. I tested it and when I use @date it displays a value 1 day before the actual date the chatbot user entered. Chatgpt suggested "The issue you are facing is likely due to JavaScript treating the timestamp as UTC and converting it to your local timezone when creating the Date object. For a more robust solution handling multiple timezones, consider using a library like moment.js or date-fns. These libraries offer more extensive and intuitive controls for handling date and time operations." I tried to ask ChatGpt but didnt get far so any further advice would be greatly received. ps. A Date picker would be magic! There's aready a feature request.
a
A datepicker would be pretty cool, I agree! Until then, you can try this code. Moment isn't included by default by Luxon is and they do about the same job.
Copy code
js
const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
console.log(userTimeZone)
const date = luxon.DateTime.fromJSDate(new Date(workflow.userDate), { zone: userTimeZone })
const formattedDate = date.toFormat('cccc, MMMM d, yyyy')
workflow.date = formattedDate
If things are still weird, look in the logs and you should see what time zone it thinks you're in. You can always manually adjust the time zone by changing the
zone
param on line 3
h
That didn't work, after some chats with the almighty GPT I got this code, which seems to be working. As I explained to ChatGPT the date is fixed, like a birthday, it makes no difference what time zone the user or server is in, the birthday is always on a set date. // Convert classStart Date object to ISO string and split to get the date const classStartDate = workflow.classStart.toISOString().split('T')[0]; // Parse the date const date = luxon.DateTime.fromISO(classStartDate); const formattedDate = date.toFormat('cccc, MMMM d, yyyy'); workflow.date = formattedDate; Looking forward to the date picker 😉
95 Views