ballingt
ballingt16mo ago

Date parsing behavior

That seems like something for us to fix, thanks for pointing it out @CodingWithJamal. Be aware that built-in date parsing with either new Date(s) or Date.parse(s) is inconsistent between runtimes (ie it works differently in Safari) so it's often not recommended.
5 Replies
ballingt
ballingtOP16mo ago
I think you can use
const d = new Date(Date.parse(s));
const d = new Date(Date.parse(s));
to get the same parsing behavior. @CodingWithJamal in your case you dont' want a date object, so using Date.parse(s) which returns the datetime in milliseconds since epoch is more direct too;
// works in Convex
const t1 = Date.parse(s);
// does the same thing as above
const t2 = new Date(Date.parse(s)).getTime()
// Doesn't work in Convex currently
const t3 = (new Date(s)).getTime()
// works in Convex
const t1 = Date.parse(s);
// does the same thing as above
const t2 = new Date(Date.parse(s)).getTime()
// Doesn't work in Convex currently
const t3 = (new Date(s)).getTime()
CodingWithJamal
CodingWithJamal16mo ago
oh okay i see. Thanks, I didnt understand the error and forgot about the runtime limitations I will try this solution
// Schedule a task to destroy the session when it expires
const time = new Date(Date.parse(args.expires)).getTime() - Date.now();
// Schedule a task to destroy the session when it expires
const time = new Date(Date.parse(args.expires)).getTime() - Date.now();
okay this works now tom thanks
CodingWithJamal
CodingWithJamal16mo ago
So im using sessions in the db to save user auth state, and when its created i wanted to create a schedule to delete the session and make sure the user has to re-login. Is this a good idea over convex cron-jobs?
No description
CodingWithJamal
CodingWithJamal16mo ago
@Tom
ballingt
ballingtOP16mo ago
Yeah I think that's fine! You could also save the time that they logged in and anytime you check for session you check whether that time was in the last 24 hours: that way you can tell the difference between a session expiration and a never-before logged in session

Did you find this page helpful?