question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using timestamp with EmbedBuilder: RangeError: Invalid time value

See original GitHub issue

Which package is this bug report for?

discord.js

Issue description

I am passing a Date object into an EmbedBuilder constructor’s timestamp field. That results in the following error:

RangeError: Invalid time value
      at Date.toISOString (<anonymous>)
      at new EmbedBuilder (S:\dev\git\github\obs-webserver\node_modules\@discordjs\builders\dist\index.js:186:54)
      at new EmbedBuilder (S:\dev\git\github\obs-webserver\node_modules\discord.js\src\structures\EmbedBuilder.js:13:5)

A little digging shows that the Date object gets eaten and turned into an empty object {} by the function toSnakeCase() in EmbedBuilder.js.

Workaround is to either create the embed without the timestamp and then use .setTimestamp() with the Date object on the resulting embed object, or use .toISOString() on the date first. Workaround was not required for versions earlier than v14.0 when this class was MessageEmbed.

Code sample

> const DiscordJs = require("discord.js")
undefined
> const embedBroken = new DiscordJs.EmbedBuilder({timestamp: new Date()})
Uncaught RangeError: Invalid time value
    at Date.toISOString (<anonymous>)
    at new EmbedBuilder (S:\dev\git\github\obs-webserver\node_modules\@discordjs\builders\dist\index.js:186:54)
    at new EmbedBuilder (S:\dev\git\github\obs-webserver\node_modules\discord.js\src\structures\EmbedBuilder.js:13:5)
> const embedWorkaround = new DiscordJs.EmbedBuilder({timestamp: new Date().toISOString()})
undefined
> embedWorkaround
EmbedBuilder { data: { timestamp: '2022-07-19T20:34:58.548Z' } }
> const embedWorkaround2 = new DiscordJs.EmbedBuilder()
undefined
> embedWorkaround2.setTimestamp(new Date())
EmbedBuilder { data: { timestamp: '2022-07-19T20:35:09.295Z' } }
>

Package version

14.0.3

Node.js version

18.6.0

Operating system

Windows 11

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

Not applicable (subpackage bug)

Which gateway intents are you subscribing to?

Not applicable (subpackage bug)

I have tested this issue on a development release

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ronclicommented, Aug 3, 2022

The argument is typed as APIEmbed

While APIEmbed is a possible type for the argument, it is not the only one defined. The typings defines the EmbedBuilder constructor as follows:

public constructor(data?: EmbedData | APIEmbed);

This should allow for either APIEmbed or EmbedData. And then, EmbedData defines timestamp as follows:

timestamp?: string | number | Date;

Date is a possible type for the value here.

It is entirely possible that a breaking change was intended here, but it’s not discussed in the v13 to v14 migration guide. This leads me to believe that a Date was intended to be accepted here, but the change to the embeds broke this particular use case.

1reaction
alperbcommented, Jul 20, 2022

I can work on this issue, @Jiralite . It’ll be my first contribution and i think this issue is quite beginner friendly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RangeError: Invalid time value - javascript - Stack Overflow
This exception occurs when the Date object contains an invalid date. ... make sure that the timestamp variable contains a valid date string....
Read more >
RangeError: Invalid time value in JavaScript | bobbyhadz
The "Uncaught RangeError Invalid time value" error occurs when calling a method on an invalid date, e.g. `new Date('asdf').toISOString()`.
Read more >
JnD2nSMLlD | SourceBin
RangeError : Invalid time value. at Date.toISOString (<anonymous>). at EmbedBuilder.setTimestamp ...
Read more >
RangeError: Invalid time value on 'date-fns' functions... Stumped.
I have two nearly identical React components(edit: also using NextJS) that don't behave as expected. My Post component displays it's date as ...
Read more >
RangeError: invalid date - JavaScript - MDN Web Docs
The JavaScript exception "invalid date" occurs when a string leading ... RangeError: Invalid time value (V8-based) RangeError: invalid date ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found