louis Schneider
louis Schneider

Reputation: 121

How to add time to a date in js

I get a date with string type from API and then I parse it to a date type so that I can use it for a count down.
I want to add 30 days to the date that I've got from API.

Here is the code that I parsed

  const time = Date.parse("2020-12-30T18:35:43");

I've already read this question and I tried to implement it

Add 10 seconds to a Date

but react does not recognize the getDate

if you need more information, please let me know

Upvotes: 0

Views: 112

Answers (2)

jean-smaug
jean-smaug

Reputation: 411

You need to wrap your parsed date with a new Date()

const time = new Date(Date.parse("2020-12-30T18:35:43"));
// As mention by other comments, this is enough
// const time = new Date("2020-12-30T18:35:43");

time.setSeconds(time.getSeconds() + 10) // 1609349753000

setSeconds and getSeconds are method of the Date, you was trying to execute them on a number.

EDIT :

Answer can be found here In a general way you should use date-fns for date manipulations ;)

Upvotes: 2

Zahid Hasan
Zahid Hasan

Reputation: 622

you can also setDate to your existing date. Check following code.

const date = new Date("2020-12-30T18:35:43");
date.setDate(date.getDate() + 30);

Upvotes: 1

Related Questions