1

I had a question, so I know to add a timestamp to a New Discord.MessageEmbed() you would use .setTimestamp(), however, I was wondering how I would go about adding x amount of time to that timestamp, i.e. if my args[1] was "12hr" (12 hours), how would I make the timestamp display a time 12 hours ahead of whoever is viewing the message?

Basically, I'm creating a giveaway-type bot, for personal use, and I'd like the footer to display the end date.

Discord Node.js

koushikmln
  • 648
  • 6
  • 23
Muel
  • 13
  • 1
  • 3

3 Answers3

3

Let's say you have let timeAdded = 12h;. There is ms package for translating that to time in milliseconds.

Also, note that .setTimestamp() method has optional parameter timestamp, that defaults to Date.now().

If you connect all this information, you'll get:

const ms = require('ms');
let timeAdded = '12h';
const NewEmbed = new Discord.MessageEmbed()
.setTimestamp(Date.now() + ms(timeAdded));

And that's it! Of course timeAdded will have to come from your args string, or in some way be fetched from the message, but you get the idea.

KifoPL
  • 981
  • 7
  • 18
1

According to the Discord.js docs, the function .setTimestamp() can take an optional input, with the default input being Date.now() if left blank.

We can parse your args[1] for seconds, minutes, hours, etc. and add them to a date object, then use that object for the .setTimestamp() function.

0

You achieve it this way:

.setTimeStamp() + (<hours you want to add> * 60 * 60 * 1000);

its not very pretty but it's a quick solution.

Alternatively

you can create a new Date and pass it as parameter to the .setTimeStamp() function, because it can take an alternative parameter

Toasty
  • 1,850
  • 1
  • 6
  • 21