I’m working on a Discord bot with a kick feature. Everything’s going well, but I’m stuck on one small detail. The bot sends a kick notification to a private #incidents channel, but the timestamp looks messy.
Here’s my current code for the notification:
let kickInfo = new discord.RichEmbed()
.setTitle('Member Removed')
.setDescription(`${kickedUser} was kicked from the server.`)
.setColor('#4287f5')
.addField('Kick Time', message.createdAt, true)
.addField('Moderator', `<@${message.author.id}>`, true)
.addField('Reason', kickReason, false);
bot.channels.get('moderationLogChannelId').send(kickInfo);
The timestamp shows up as a long string of numbers and letters. I’d like to change it to a more readable format like DD/MM/YYYY HH:MM AM/PM in AEST (Australian Eastern Standard Time).
Any ideas on how to format the date and time correctly for the kick notification? Thanks!
I’ve encountered similar timestamp issues in my Discord bot projects. While Moment.js is a popular choice, I’ve found that using the native JavaScript Date object can be more lightweight and just as effective. Here’s an approach I’ve used:
function formatDate(date) {
const options = {
timeZone: 'Australia/Sydney',
day: '2-digit', month: '2-digit', year: 'numeric',
hour: '2-digit', minute: '2-digit', hour12: true
};
return new Date(date).toLocaleString('en-AU', options) + ' [AEST]';
}
// In your kick notification code
.addField('Kick Time', formatDate(message.createdAt), true)
This method gives you the desired format without additional dependencies. It’s also more maintainable in the long run, as you won’t need to update external libraries. Just remember to adjust the timeZone if daylight savings becomes a factor.
As someone who’s worked on a few Discord bots, I can relate to your timestamp formatting issue. Here’s a solution that worked well for me:
Instead of using message.createdAt directly, you can use the Moment.js library to format the timestamp. First, install it with npm install moment-timezone, then use it like this:
const moment = require('moment-timezone');
// In your kick notification code
.addField('Kick Time', moment(message.createdAt).tz('Australia/Sydney').format('DD/MM/YYYY hh:mm A'), true)
This will give you the exact format you’re looking for, in AEST. The ‘Australia/Sydney’ timezone ensures it’s in AEST.
One tip: consider adding the timezone abbreviation to avoid confusion for users in different regions. You can do this by adding ’ [AEST]’ to your format string.