【问题标题】:message.createdAt gives too much info (discord.js)message.createdAt 提供了太多信息 (discord.js)
【发布时间】:2020-11-22 00:48:39
【问题描述】:

使用 message.createdAt 时,它会返回关于消息发送时间的大量信息。 示例输出:Sat Aug 01 2020 12:23:56 GMT+0200 (Central European Summer Time)

有没有办法将这些庞大的信息缩短为:Sat Aug 01 2020 12:23:56

我想用 " " 分割它,然后连接数组的前 5 个元素,有没有更好的主意:

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

【问题讨论】:

标签: javascript node.js time discord discord.js


【解决方案1】:

既然 Discord API 属性 createdAt 返回一个 Date 对象,为什么不直接将 Date 转换为 localeString() 呢?

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


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

【讨论】:

    【解决方案2】:

    尝试使用moment

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

    Moment's Format

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-23
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 2021-12-22
      • 2022-07-29
      • 2017-12-28
      相关资源
      最近更新 更多