'Get Uptime of Discord.JS bot
I am right now making a Discord bot command for runtime, I was wondering what the most compacted (and still correct) way of doing an runtime to catch how long the bot has actually been online and return it in 24hr format.
Solution 1:[1]
Much better solution
const moment = require("moment");
require("moment-duration-format");
const duration = moment.duration(client.uptime).format(" D [days], H [hrs], m [mins], s [secs]");
console.log(duration);
//Output = 1 hr, 16 mins, 8 secs
Solution 2:[2]
Here's a very simple solution that returns a human-readable string. It uses the pretty-ms module.
const prettyMilliseconds = require("pretty-ms");
message.channel.send(`Uptime: ${prettyMilliseconds(client.uptime)}`)
// 15d 11h 23m 20s
Solution 3:[3]
You could use
var startingTime = Date.now();
at the start of your code and when you want to use it, use
var uptime = startingTime - Date.now();
Then use the accepted code, but use uptime instead of client.uptime. This is for non-discord.js.
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 | Cody Gray |
| Solution 2 | Beatso |
| Solution 3 | Athanasios Emmanouilidis |
