Reputation: 422
I have two variables,
let year = 2003;
let passedDays = 275;
I need to get the Date
as 2003-10-02
from those two variables.
How can I do it without writing a hundred lines of if statements.
Thanks in advance.
Upvotes: 2
Views: 48
Reputation: 15247
Build a date
object with the given year, for January, the first, then add the needed days :
let year = 2003;
let passedDays = 275;
// note that months starts with zero
// |
// V
let date = new Date(year, 0, 1);
// Make sure passedDays is a number, not a string containing a number,
// otherwise you'll get wrong result (string concat)
// |
// V
date.setDate(date.getDate() + +passedDays);
console.log(date);
Upvotes: 7