DEV Community

Joshua Amaju
Joshua Amaju

Posted on

Manipulating dates in Typescript

The correct way to subtract or add dates when using Typescript

const diff = new Date().valueOf() - date.valueOf();
Enter fullscreen mode Exit fullscreen mode

valueOf gives you the date as number which satisfies Typescript.

Doing the following produces a warning

const diff = new Date() - date;
// Error: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay