Search code examples
javascriptdatenew-operatorgetdate

d.setDate(d.getDate()+7) returns a bunch of numbers


When i run the code bellow, which parses the current date + 7 days, returns huge number on the date variable;

let d = new Date();
let n = d.setDate(d.getDate()+7);
let m = d.getMonth()+1;
let o = d.getFullYear();
let dateOp = n + "/" + m + "/" + o;

dateOp;

// returns "1609772260625/1/2021"

Solution

  • From the docs, setDate returns the millisecond difference between the date and the UNIX epoch. So, set the date first, then get the date again:

    d.setDate(d.getDate() + 7);
    let n = d.getDate();
    let m = d.getMonth()+1;
    let o = d.getFullYear();
    let dateOp = n + "/" + m + "/" + o;
    
    dateOp;