0

i'm looking to handle a script wich has to add 10 years to the current timestamp in javascript...

But when i try this:

Date.now() + /* 10 years in seconds */ 315569520

It doens't work... Is there anyway to implement it ?

Loth237
  • 5
  • 2

4 Answers4

1

Do like:

const dt = new Date;
dt.setFullYear(dt.getFullYear()+10);
console.log(dt.toString());
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

plz check this url. You can find your solution. https://www.w3resource.com/javascript-exercises/javascript-date-exercise-41.php

camper
  • 51
  • 8
0

it's very simple you can achieve in two line code:

`let timestamp = new Date();

timestamp.setFullYear(timestamp.getFullYear() + 10);
console.log(timestamp);`

-1

You need to use a Date constructor to create the date from the value you calculated.

new Date(Date.now() + 315569520 * 1000)

You need to add the value of 10 years in milliseconds, and then pass the new value to a Date constructor

  • The milliseconds in 10 years depends on the years in question as there may be one, two or three leap years involved. – RobG Jun 14 '21 at 03:46