Fiddle Freak
Fiddle Freak

Reputation: 2051

add large string milliseconds with number milliseconds

when I store Date.now() in the database as a bigint (postgresql), making a recall for the value returns a string instead of a number (due to javascript inability to handle large numbers).

Is there a way I can utilize the javascript Date library to compare the string of milliseconds with the Date.now() milliseconds?

Something like this...

const oldDate = "1590367617261"; // returned as string from database
const timout = 5 * 1000; // ms

console.log(oldDate + timout);

Expected Output:

1590367622261

Actual Output:

15903676172615000

Upvotes: 0

Views: 42

Answers (2)

Kim B
Kim B

Reputation: 61

Type the value of oldDate without ""

const oldDate = 1590367617261;
const timeout = 5 * 1000; // ms
console.log(oldDate + timeout);

Upvotes: 0

Damnik Jain
Damnik Jain

Reputation: 434

Parse oldDate to Int before adding it

const oldDate = "1590367617261";
const timout = 5 * 1000; // ms

console.log(parseInt(oldDate)+ timout);

Adding string to int will result in string concatenation.
Adding int to int will result in addition operation.

Upvotes: 3

Related Questions