Omlahid / NaNoWriMo-Discord-Bot

Discord Bot specifically for NaNoWriMo
4 stars 4 forks source link

Consider Moment.js for time handling #3

Open francoislg opened 8 years ago

francoislg commented 8 years ago

http://momentjs.com/

npm install moment

Then in your code:

var moment = require("moment");

Next you can do all the stuff in the doc!

Would help you with everything "Date" and "Time" :)

Omlahid commented 8 years ago

Sounds good, but I'd like to have as little dependencies as possible, to ease implementation. I will however consider it. Thanks!

LordKacem commented 4 years ago

If you have a time, such as .addField("Time:", message.createdAt);, how would you implement Moment into it? Something like .addField("Time:", moment().createdAt);? Thanks in advance.

I know that the post is 2 years old but my intention is to help anyone facing the same issue. Let's use this as an example, you want to change the format of the variable "guild.createdAt" in an embed:

.addField("Creation Date:", guild.createdAt);

You would change it to:

.addField("Creation Date:", moment(guild.createdAt).format('PUT YOUR FORMAT HERE'));

Example:

.addField("Creation Date:", moment(guild.createdAt).format('MMMM Do YYYY, h:mm:ss a'));

Will return: February 11th 2019, 8:53:45 am

So overall, this is how it is used:

moment(YOUR DATE/TIME VARIABLE).format('FORMAT');

Note: you can find a simple guide for Moment date/time formats here => https://www.codegrepper.com/code-examples/r/discord.js+moment+date

Considering that this comment is quite old, this will not work for lower versions, only discord js v12.