Get array of dates from an initial and final timestamp

Let's assume that you have an initial and final date, and you want to return an array of in-between dates in DD/MM/YYYY format.

Using vanilla JavaScript

javascript
const getDatesBetween = (from, to) => {

    let dates = []

    let ini = new Date(from)
    let end = new Date(to)

    while(ini <= end){

        dates.push(ini.toLocaleDateString(undefined, { year: 'numeric', month: '2-digit', day: '2-digit' })) && ini.setDate(ini.getDate() + 1)

    }

    return dates

}

Using moment.js

javascript
const getDatesBetween = (from, to) => {

    let dates = []

    let ini = moment(from)
    let end = moment(to)

    while(ini <= end){

        dates.push(ini.format('DD/MM/YYYY')) && ini.add(1, 'days') 

    }

    return dates

}

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.