Skip to main content

Snowflake to Time

This code snippet extracts the underlying time of snowflakes (Discord IDs).

Sometimes, you want to get the time that, say, a user created their account / a channel was created. If you look in the documentation, that's not something that is accessible in templates. However, most Discord structures have an ID. Those IDs all have one thing in common - they use the snowflake format. This format contains a timestamp, which you can extract to get the time when it was created.

Licensed under the terms of the Unlicense.

info

For more information, see this section on the Discord developer documentation.

Code

{{/*
Extracts the time that a given Discord snowflake was created.
See <https://yagpdb-cc.github.io/code-snippets/snowflake2time> for more information.

Licensed under the terms of the Unlicense.
Author: jo3-l <https://github.com/jo3-l>
*/}}

{{/* Let $snowflake be the ID. */}}
{{ $snowflake := .User.ID }}

{{/* snowflake >> 22 can be evaluated as snowflake / 2 ^ 22. 2 ^ 22 is 4194304. */}}
{{ $time := div $snowflake 4194304 }}

{{/* Multiply by 10^6 to convert milliseconds to nanoseconds */}}
{{ $time = mult $time 1000000 }}

{{/* Convert to duration so we can pass it as an argument to time.Add, which expects a duration and returns a time */}}
{{ $time = toDuration $time }}

{{ $time := .DiscordEpoch.Add $time }}

Usage

First, add the code snippet above:

{{/* code snippet goes here */}}

Next, change the value of $snowflake to your input value. Say we wanted to use .Guild.ID rather than .User.ID:

{{/* Let $snowflake be the ID. }}
- {{ $snowflake := .User.ID }}
+ {{ $snowflake := .Guild.ID }}
{{/* rest of code snippet goes here */}}

You can now access the time when $snowflake was created using $time:

{{/* code snippet goes here */}}
Time `$snowflake` was created: {{$time}}

Author

This code snippet was written by @jo3-l.