#Uncaught Error: `new Date()` with non-number arguments is not supported.

77 messages · Page 1 of 1 (latest)

pearl cargo
#

I'm seeing this in a mutation where I'm trying to use date-fns timezone functions - is this specific to the convex runtime?

#

This does seem specific to convex. What I need to do is convert the utc time from new Date() to a given timezone. date-fns-tz utcToZonedTime() does this, but doesn't seem to work in this context. Let me know if anyone has an approach here.

pearl cargo
#

For my use case I think I'm just going to bypass all this and pass the current date in ISO to the backend since I don't actually need the time. But a solution here may be helpful for others in the future.

brave hull
#

For now: related thread from #general (we should do a better job of moving those to here for searchability!): #1171508877095874570 message

brave hull
#

Is the library function failing internally? The work-around in that thread assumes you are the one passing the string to date

pearl cargo
#

Yeah the library calls new Date() internally

#

but passes in a string

brave hull
#

Gotcha - we're chatting internally about the best course of action here. My understanding is that new Date has inconsistent behavior so we were trying to avoid it. From https://maggiepint.com/2017/04/11/fixing-javascript-date-web-compatibility-and-reality/:

When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as local time. This is due to a historical spec error that was not consistent with ISO 8601 but could not be changed due to web compatibility.
Maybe it's for other reasons too. but a library should be trusted to do the right thing, so we should probably offer some implementation rather than nothing

pearl cargo
#

Temporal will save us. But in like 2028 at this rate.

pearl cargo
#

@brave hull update - I just upgraded date-fns itself, the core lib, from 2.x to 3.x. One of the key features is that it once again accepts string dates, allowing me to get rid of all of the date parsing and stringifying I was doing all over my app - but this error is now popping up everywhere.

From a prioritization standpoint, I'd offer that the convex runtime is more or less incompatible with the latest version of the top dates library.

lusty stratus
#

I'm getting the same when importing some data with let let createdAt = '2020-12-07T23:34:56.860Z' that i'm using new Date(createdAt) . Now i'm trying with Date.parse(createdAt)

lusty stratus
#

that worked

pearl cargo
#

Just giving this another bump, bit of a thorn in the side.

#

An opinion on the matter of how best to approach this given the flakiness of the API, it feels like matching Node's behavior is the most straightforward path. It fixes any library compatibility issues, brings exactly the same caveats folks would already have encountered in Node, and introduces no special cases that Convex would need to provide extra support for, as nothing Convex specific is happening.

pearl cargo
pearl cargo
#

For anyone else that runs into this, here's my solution for timezone support that should be consistent across various environments, including react native:

  • date-fns for general date handling
  • Temporal (the TC39 proposal, not the platform) for timezone support (will probably move to Temporal for everything at some point)
  • using this polyfill, which feels closer to production readiness than the "main" one: https://www.npmjs.com/package/temporal-polyfill
pearl cargo
#

I keep forgetting Convex has a competitor named Temporal. I'm not talking about that Temporal! lol

pearl cargo
#

Update in the never ending saga: due to an incomplete iOS implementation of formatToParts() in the Intl API, the React Native team decided to disable the method altogether, which breaks the Temporal polyfill. They have plans to address but aren't yet known to be working on it.

pearl cargo
manic merlin
#

@pearl cargo I tried making the changes to package using patch-package but its not reflecting when im runing locally i added multiple console log still cant see console log also I installed it properly and rerun the app still cant see the console logs

pearl cargo
#

are you using npm, and do you have date-fns-tz 3.1.3

manic merlin
#

yes

#

"date-fns": "^3.6.0",

#

"date-fns-tz": "^3.1.3",

pearl cargo
#

Did you add the postinstall script

#

I haven't seen patch-package silently fail to work in the times I've used it, so just asking basic questions here

manic merlin
#

Yes

pearl cargo
#

did you actually run patch package? the postinstall script runs if you run npm install, otherwise you need to do npx patch-package.

manic merlin
#

yes i did it created a file also then i did npm install and npm run dev

#

i also tried deleting nextjs cache folder

#

now im moving all my logic to action so i can use this package

pearl cargo
#

yeah not sure why patch package isn't working for you, sorry I couldn't be of more help there

gaunt vine
#

I got this issue on Convex too with dayjs
there is no solution at the moment 😢

leaden nacelle
#

We're still working on a fix, it's a pickle, but we should get there soon-ish.

gaunt vine
#

@leaden nacelle sorry to mention you, but do we have quick workaround or draft I can try to do hot fix on my project?

leaden nacelle
#

Reading this thread, I think you can try @pearl cargo's fix of patching the library to use Date.parse() instead of new Date() directly (you might need to expand the patch).

You can make the change manually in your node_modules to see whether it helps (you can also console.log from node_modules to debug what's going on)

gaunt vine
#

well, I am using dayjs, but I got the point

the coming fix will fix Convex itself or a workaound as above?

leaden nacelle
#

It will fix the Convex runtime itself

gaunt vine
#

@leaden nacelle thanks, do we have estimation time which fix released?

leaden nacelle
#

No estimate, will update here when we do

gaunt vine
#

ok thank you for your info

pearl cargo
#

🙏

tacit valley
pearl cargo
#

No it's a plea 😅

#

just a bump to keep it non-stale. still feeling this one.

manic merlin
#

hahah nice way of bumping

pearl cargo
#

lol it worked!

#

maybe

manic merlin
#

hopefully

#

xD

tacit valley
#

@pearl cargo we good now? #announcements message

manic merlin
#

lets gooooo

#

refacctoring started

final aurora
#

Should date-fns v4 be supported with this change? In particular @date-fns/tz ?

I didn't receive any Convex runtime error when trying to import from @date-fns/tz so I assumed it should work, but it doesn't seem to be working correctly.

 import { TZDate, tz } from '@date-fns/tz';
 const melbourneTime = new TZDate(1728222554737, 'Australia/Melbourne');
  
  console.log(melbourneTime.toString());
  // Convex runtime: 'Sun Oct 06 2024 13:49:14 GMT+0000 (Coordinated Universal Time)'
  // locally using vitest: 'Mon Oct 07 2024 00:49:14 GMT+1100 (Australian Eastern Daylight Time)'
final aurora
#

It works as expected if code is placed within a Convex action, just not within a query/mutation.

pearl cargo
#

Convex runtime uses UTC, your local will use local time. The action/query difference is not something I would expect, though.

#

Oh I missed that you’re setting the timezone nvm

#

Action on convex runtime works or is it using Node?

final aurora
#

I don't think the system time matters when using TZDate?. Yep, it works fine within a Convex action with "use node".

pearl cargo
#

Yeah I missed that you were setting the timezone. Sounds like v4 is relying on a platform API that works differently between the Convex runtime and Node, would have to trace through the function you’re calling to see what that might be.

leaden chasm
#

it sounds like we might need to do something different here, I filed this internally but if someone does sees what part of date-fns is broken by TZDate here we can get to it sooner

final aurora
#

Thanks. I can work around the lack of TZDate support in mutations, but using Convex actions.

However, for Convex queries I have no suitable workaround because they can't be switched out with actions as easily - live subscriptions would be broken. I could setup a HTTP endpoint (using an action) but is starts to be loose a lot of the Convex benefits quickly.

Essentially, I want to fetch all orders for the current day only (dependent on my merchant.timeZone setting). Order date is store as a timestamp. Any suggestions how I do this using a Convex query?

lyric tapir
#

Would it work to calculate the timestamp for the current day outside of the query—e.g. using TZDate on the front end—then pass the value to the query function to get matching records using an index?

final aurora
#

That’s a good idea @lyric tapir I’ll give it a go, thanks

final aurora
#

I've resolved the TZDate issue for Convex queries by passing args from the client side.
However, it's still a problem for internal mutations triggered on a schedule.
I'm wondering if Convex will support date-fns v4 with @date-fns/tz inside mutations?

brave hull
# final aurora I've resolved the `TZDate` issue for Convex queries by passing args from the cli...

If I understand correctly, you're looking to get the date string from a timestamp with a date string? This function gets the same result in queries & mutations for me:

function formatDateWithTimezone(timestamp: number, timeZone: string): string {
  const date = new Date(timestamp);

  const options: Intl.DateTimeFormatOptions = {
    year: "numeric",
    month: "numeric",
    day: "numeric",
    timeZone: timeZone,
  };

  return date.toLocaleString("en-US", options);
}
const timestamp = 1728222554737;
const timeZone = "Australia/Melbourne";

-> '10/7/2024'

final aurora
brave hull
#

Gotcha. Yeah that's very reasonable. Sorry it isn't fixed yet. I'll re-raise internally

final aurora
#

The date-fns-tz package works fine, which uses this Intl API but is intended for date-fns v3, not v4.

brave hull
#

So it's possible, but a footgun that @date-fns/tz doesn't work as expected with date-fns v4?

final aurora
#

I think it is possible to use date-fns-tz with date-fns v4 but I'm not 100% confident it will always work as expected. It seems the new package (@date-fns/tz) doesn't use Intl API.

final aurora
#

I looked into this a bit further and date-fns-tz lists date-fns v4 as a peer dependency so it's probably the best solution for now.
https://github.com/marnusw/date-fns-tz/blob/master/package.json#L131
The down side of this approach is that it tends to trip up the AI because I'm using @date-fns/tz on the frontend, so supporting @date-fns/tz within Convex mutations and queries would be ideal.

fossil frigate
#

Ran into this today, would be good if this could get another look

leaden chasm
#

Oh still? @fossil frigate what's the error you got, and what code did it come from?

#

I think we fixed this, so might be a new issue but regardless will fix