【问题标题】:How to get the the createdAt timestamp without 'GMT+0000 (Coordinated Universal Time)'?如何在没有“GMT+0000(协调世界时)”的情况下获取 createdAt 时间戳?
【发布时间】:2021-12-30 03:26:10
【问题描述】:

我到处寻找这个,但我没有得到正确的答案。我只想设置它没有 CUT 时间。

{name: "Registered:", value: `${user.createdAt}`},
    
{name: "Joined:", value: `${message.guild.joinedAt}`}

目前显示如下: click here to see.

有什么办法可以去掉吗?

【问题讨论】:

    标签: javascript node.js discord.js repl.it


    【解决方案1】:

    假设属性是 JS Date 实例,您可以使用以下函数之一来执行此操作:

    function formatDateV1 (date) {
      return date.toLocaleString(undefined, {dateStyle: 'medium', timeStyle: 'medium', hour12: false});
    }
    
    function formatDateV2 (date) {
      return `${date.toDateString()} ${date.toTimeString().slice(0, 8)}`;
    }
    
    const date = new Date();
    const user = {createdAt: date};
    const message = {guild: {joinedAt: date}};
    
    const registerV1 = {name: "Registered:", value: formatDateV1(user.createdAt)};
    console.log('registerV1', registerV1);
    
    const joinV1 = {name: "Joined:", value: formatDateV1(message.guild.joinedAt)};
    console.log('joinV1', joinV1);
    
    const registerV2 = {name: "Registered:", value: formatDateV2(user.createdAt)}
    console.log('registerV2', registerV2);
    
    const joinV2 = {name: "Joined:", value: formatDateV2(message.guild.joinedAt)}
    console.log('joinV2', joinV2);

    【讨论】:

      【解决方案2】:

      是的,可以删除它

         {name: "Registered:", value: `${user.createdAt.toDateString()}`},
              
         {name: "Joined:", value: `${message.guild.joinedAt.toDateString()}`}
      

      您应该添加toDateString()以避免GMT或CUT时间

      【讨论】:

      • 但是我现在如何设置正常时间-
      • .toUTCString 将显示没有 GMT +0000 的 UTC 时间
      • 非常感谢。但是-我如何删除 GMT 这个词然后?。这就是它的显示方式Sun, 01 Nov 2020 11:20:37 GMT
      • 可以安装moment包,这样写moment(user.createdAt).format("DD/MM/YYYY, HH:MM"),但是需要像const moment = require("moment")这样导入这个包
      • 如果你有的话,我可以得到你的不和谐吗?我想问更多问题;-;
      猜你喜欢
      • 1970-01-01
      • 2020-10-13
      • 2018-09-16
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 2021-10-27
      • 2018-02-12
      相关资源
      最近更新 更多