A
A
aftar2019-04-23 20:14:57
JavaScript
aftar, 2019-04-23 20:14:57

How to make an array of dates from an array of integers?

There is an array . It is necessary to calculate the date from each number in it and write a new array.
var date = [-2, -5, -7, -11, -19, -20];

//-Результат
["21.04.2019 г.", "18.04.2019 г.", "16.04.2019 г.", "12.04.2019 г.", "4.04.2019 г.", "3.04.2019 г."]


var date = [-2, -5, -7, -11, -19, -20];
    var dateNew =[];

    for (var i = 0; i < date.length; i++) {
        var d = new Date(new Date().getTime() +date[i] * 24 * 60 * 60 * 1000);
        var month = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");

        var newDate = (+d.getDate() + "." + month[d.getMonth()] + "." + d.getFullYear() + " г.");

        dateNew.push(newDate)

       	//localStorage.dateSave = JSON.stringify(dateNew);
    }


So far, the output is such a picture.5cbf47edb5bce265242538.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanKud, 2019-04-23
@aftar

var date = [-2, -5, -7, -11, -19, -20];

const arr = date.map(n => {
  const d = new Date(new Date().getTime() - (-n * 86400000));
  return d.getDate() + '.' + d.getMonth() + '.' + d.getFullYear() + ' г.';
});

console.log(arr);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question