You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js v14嵌入中添加12小时时间戳出错求助

解决Discord嵌入中时间戳加12小时的错误问题

问题原因

你的代码计算时间戳时括号位置错误,导致单位混乱:
Math.floor(new Date().getTime() + ms(timeAdded)/ 1000)
new Date().getTime()和ms(timeAdded)返回的都是毫秒数,但你仅将ms(timeAdded)除以1000转成秒,再与毫秒数相加,最终得到的是「毫秒+秒」的混合数值,数值被大幅放大,因此显示出错误的远古时间。

修复后的代码

将当前时间与新增时间的毫秒总和整体除以1000转成Discord所需的Unix秒数,修正括号位置:

const ms = require('ms');
let timeAdded = '12h';
const embed = new EmbedBuilder()
                    .setTimestamp()
                    .setColor('3564fa')
                    .setFooter({ text: `💗 Merci de soutenir le projet ${interaction.user.tag}`, iconURL: client.user.avatarURL()})
                    .setDescription(`Vous pouvez votez pour ${client.user} en votant **[ici](https://top.gg/fr/bot/931808039231713301/vote 'GrayBot vote')**. Vous pourrez revoter dans <t:${Math.floor((new Date().getTime() + ms(timeAdded)) / 1000)}:R>`)
                    
return interaction.reply({ embeds: [embed]})

关键说明

  • Discord的<t:timestamp:R>格式要求传入Unix时间戳(秒数),而非毫秒数
  • ms('12h')返回12小时对应的毫秒数,必须和当前时间的毫秒数相加后,整体除以1000转成秒,再取整才能得到正确的时间戳

内容的提问来源于stack exchange,提问作者Aubin PRIEUR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:31:50