1

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);
grafpatron
  • 514
  • 1
  • 3
  • 20
  • I think you need to set as date object and look at formatting it https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date . It is standard date with timezone. Not really too much info – Suraj Rao Aug 01 '20 at 10:42
  • 1
    Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Suraj Rao Aug 01 '20 at 10:48

2 Answers2

2

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);
Melchia
  • 16,699
  • 16
  • 70
  • 93
1

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

Rulavi
  • 135
  • 5