How to turn Date() method into a Discord hover able timestamp

Hello!

I am making a Discord bot on glitch.com and I was trying to convert a Date() function into a Discord hover able timestamp.

I know there are websites that can convert it, but I want to be able to convert it in my code because each timestamp will be different.

This is what I mean by the Discord timestamp.
Screenshot 2022-04-20 155451

This is my code:

var user = await Levels.fetch(mentionedMember, interaction.guildId, true);
    var lastUpdatedDate = new Date(user.lastUpdated).toLocaleDateString("en-US");
    var lastUpdatedTime = new Date(user.lastUpdated).toLocaleTimeString("en-US");
    return `<@${user.userID}> is **level ${user.level}** with **${user.xp} xp**. Last updated on ${lastUpdatedDate} at ${lastUpdatedTime} *(PST)*.`

The output is this:

The main reason I want to turn this into a Discord timestamp is so it will automatically adjust to the user’s time zone.

Thank you!

1 Like

Nevermind, I solved the problem myself.

But I’ll say the answer to help other people.

I converted the Date() into a unixtimestamp by using this line of code:

const unixTimestamp = Math.floor(lastUpdatedTime.getTime() / 1000);

Then I could use it as a Discord timestamp by using this:

<t:${unixTimestamp}:R>
1 Like