How to format date and time in Discord bot embed for Australian timezone?

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!

hey mate, i’ve dealt with this before. you’ll wanna use the moment-timezone library. install it with npm, then import it. use it like this:

const moment = require('moment-timezone');
// ...
.addField('Kick Time', moment().tz('Australia/Sydney').format('DD/MM/YYYY hh:mm A'), true)

that should sort ya out for AEST. lemme know if ya need anymore help!

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:

const formatter = new Intl.DateTimeFormat('en-AU', {
  timeZone: 'Australia/Sydney',
  day: '2-digit',
  month: '2-digit',
  year: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  hour12: true
});

const kickTime = formatter.format(message.createdAt);

.addField('Kick Time', kickTime, true)

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

Then in your code:

const { format, utcToZonedTime } = require('date-fns-tz')

// In your embed creation:
const kickTime = utcToZonedTime(message.createdAt, 'Australia/Sydney')
const formattedTime = format(kickTime, 'dd/MM/yyyy hh:mm a', { timeZone: 'Australia/Sydney' })

.addField('Kick Time', formattedTime, true)

This should give you exactly what you’re after without the bloat of moment.js. It’s also more future-proof as date-fns is actively maintained.