'How to make a Discord bot tell the current date and time on command?

I want to have my Discord bot tell me the current date and time on a simple command, so if I say "whats the time?" it will tell me. Currently my only solution is having the bot send a link to the current time on Google, but that's not ideal, I'd like it to actually show it as a message in the channel.

Thanks



Solution 1:[1]

You can use the built-in Date class to generate a datetime string.

// inside a command

const currentDate = new Date();
message.channel.send(currentDate);

The example above would result in the bot replying with something like "Sat Dec 12 2020 15:40:06 GMT+0100 (Central European Standard Time)".

If you want to have a more human-friendly string, you can turn it into a locale string using toLocaleString()

message.channel.send(currentDate.toLocaleString());

This would result in something like "12/12/2020, 3:41:58 PM", the date and hour formats depending on the locale you specified, or the server locale, if not specified.

Solution 2:[2]

Also - you can create two different 'time' and 'date' commands, and instead of writing "toLocalString" just write "toLocalDateString" for the date and "toLocalTimeString" for the time.

Solution 3:[3]

You can create a discord bot in the language you prefer, there are SDKs for multiple languages for them. Also, there are ways to create date object in multiple languages too.

Here is an example in Node.JS.

  1. How to create a discord bot. Node.JS Discord bot
  2. While replying to a message, create a date object. Return it in the reply.

Here is the sample code:

const Discord = require('discord.js')
const client = new Discord.Client()

client.on('ready', () => {
    console.log('Bot is now ready to communicate with discord server');  
});

client.on('message', (receivedMessage) => {
    // Prevent bot from responding to its own messages
    if (receivedMessage.author == client.user) {
        return
    }
    
    // Check if the bot's user was tagged in the message
    if (receivedMessage.content.includes(client.user.toString())) {
        // Check contents if client wants date
        if(receivedMessage.content == '!date') {
            let date = new Date();
            // Send date
            let content = date.getDate() + '/' + date.getMonth() + '/' + date.getFullYear();
            receivedMessage.channel.send(content)
        }
        // Check contents if client wants time
        if(receivedMessage.content == '!time') {
            let date = new Date();
            // Send time
            let content = date.getHours() + ':' + date.getMinutes() + ';' + date.getSeconds();
            receivedMessage.channel.send(content)
        }
    }
})
client.login("XXXXXXXXXXX") // Replace XXXXX with your bot token

Also you can check out mee6, if you don't want to code much.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xeoth
Solution 2
Solution 3 Trishant Pahwa