Discord Bot Timestamps in JavaScript (discord.js) & Python (discord.py)
By Alex Vance•Updated 2026-09-25•8 min read
Writing bots in discord.js or discord.py? Stop manually string-concatenating `<t:${time}:R>`. Both SDKs ship with native, type-safe helpers that format dates cleanly and protect against timezone bugs.
Implementing Timestamps in discord.js (v14+)
In modern discord.js v14, you do not need to stitch strings together. Discord.js provides the `time()` helper and the `TimestampStyles` enum directly. They give you full TypeScript autocomplete and ensure you never pass an invalid flag.
typescript
import { EmbedBuilder, time, TimestampStyles } from 'discord.js';
const targetDate = new Date('2026-09-25T20:00:00Z');
// Create formatted timestamp strings
const relativeTime = time(targetDate, TimestampStyles.RelativeTime); // <t:1727280000:R>
const longDateTime = time(targetDate, TimestampStyles.LongDateTime); // <t:1727280000:F>
const embed = new EmbedBuilder()
.setTitle('Community Game Night')
.setDescription(`Join us ${relativeTime} on ${longDateTime} for our server tournament!`)
.setColor(0x5865F2);Implementing Timestamps in discord.py (v2+)
In Python's `discord.py` library, you can use the built-in `discord.utils.format_dt()` function, which takes standard Python `datetime` objects and style flags.
python
import discord
from discord.ext import commands
from datetime import datetime, timezone
bot = commands.Bot(command_prefix='!', intents=discord.Intents.default())
@bot.command()
async def countdown(ctx):
# Create a timezone-aware UTC datetime
event_time = datetime(2026, 9, 25, 20, 0, 0, tzinfo=timezone.utc)
# Format with discord.utils.format_dt
relative_str = discord.utils.format_dt(event_time, style='R')
full_str = discord.utils.format_dt(event_time, style='F')
await ctx.send(f'The event will take place {relative_str} ({full_str})!')Common Pitfall: Server Time vs User Time
Here is a classic bug in reminder bots: a user in California types `/remindme 8pm`. Your bot host is in Frankfurt (UTC+1). If your code simply parses '8pm' using local system time, your reminder fires 9 hours early or late. Always capture the user's timezone offset or store everything in UTC.
Frequently Asked Questions
No. Autocomplete and command choices cannot dynamically render timestamps. The tag will display as raw text inside the command menu.