pragmatrick
pragmatrick

Reputation: 635

message.createdAt gives too much info (discord.js)

When using message.createdAt, it returns a huge string of information about the time a message was send. Example output: Sat Aug 01 2020 12:23:56 GMT+0200 (Central European Summer Time)

Is there a way to shorten this huge info into just: Sat Aug 01 2020 12:23:56?

I thought of splitting it by " " and then concat the first 5 elements of the array, is there a better idea than:

const time = msg.createdAt.toString();
const time_array = time.split(" ");
const time_str = time_array.reduce((sum, element) => sum+element);

Upvotes: 1

Views: 2766

Answers (2)

Rulavi
Rulavi

Reputation: 183

Try using moment library

const dateCreated = moment(msg.createdAt)
console.log(dateCreated.format("DD/MM/YYYY LTS"))
// 01/08/2020 3:07:44 PM

Moment's Format

Upvotes: 1

Melchia
Melchia

Reputation: 24244

Since Discord API attribute createdAt returns a Date Object, why don't you just convert your Date to localeString() ?

const msg = { createdAt : new Date() };


// Demo
const time = msg.createdAt.toLocaleString();
console.log(time);

Upvotes: 2

Related Questions