I’m working on a Discord bot with a kick feature. Everything works fine, but I’m stuck on formatting the date and time in the embed that gets sent to the #incidents channel. Right now, it looks messy and hard to read.
Here’s what my code looks like:
const kickEmbed = new discord.RichEmbed()
.setTitle('Member Kicked')
.setDescription(`${kickedUser} was removed from the server.`)
.setColor('#4287f5')
.addField('Kick Time', message.createdAt, true)
.addField('Kicked by', `<@${message.author.id}>`, true)
.addField('Reason', kickReason, false);
incidentsChannel.send(kickEmbed);
The problem is with the ‘Kick Time’ field. It’s using message.createdAt, but the output is confusing.
I want to change it to show: DD/MM/YYYY HH:MM AM/PM in Australian Eastern Standard Time (AEST).
Any ideas on how to format the date and time correctly for my timezone? Thanks!
Having worked on several Discord bots, I can suggest using the Intl.DateTimeFormat API. It’s built into JavaScript and doesn’t require any extra libraries. Here’s how you can implement it:
This approach is lightweight, doesn’t require additional dependencies, and handles daylight saving time automatically. It’s also locale-aware, so it’ll format the date according to Australian conventions.
As someone who’s built a few Discord bots, I can attest that date formatting can be a real pain. I’ve found that the date-fns library is incredibly lightweight and perfect for this kind of task. Here’s how I’d approach it:
First, install date-fns with npm:
npm install date-fns date-fns-tz