-2

I saw this question , so what is did was :

let date = 1507135378538;

let date2 = new Date(date).getTime(); //NaN
let dateIn30Days = date2 + (30 * 24 * 60 * 60 * 1000);//NaN

What I'm doing wrong? thanks

Marry G
  • 377
  • 1
  • 3
  • 16

1 Answers1

2

You could use Date#setTime to set a time with epoch/UNIX time.

let date = 1507135378538;
let date2 = new Date;

date2.setTime(date);
console.log(date2);                             // 2017-10-04T16:42:58.538Z

date2.setTime(date + 30 * 24 * 60 * 60 * 1000);
console.log(date2);                             // 2017-11-03T16:42:58.538Z
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I was having an issue with handling UNIX time returned from a database query, and formatting as date in JS. A piece of this solution did the trick for me. Particularly, assigning date to date2 with .setTime. Other, more direct attempts (such as assigning new Date(my-unix-time) would result in error. – like2think Sep 22 '22 at 19:06