0

I have array full of dates. I would like to get 3 separate information from this one array.

  1. Number of dates before today. To be more specific dates before now, so I need to include hours, minutes and seconds.
  2. Number of dates between now and the end of the current date
  3. Number of dates with tomorrow date

Example date from array:

"02/11/2019 11:22:28",
"05/14/2019 12:17:49",
"06/06/2019 09:00:00",
"05/02/2019 11:05:30"

I believe that moment.js could help here, but I am unable to make it work.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
NiTjSefni
  • 85
  • 10
  • 4
    What have you tried? Post that code along with the error/issues in it. – Prerak Sola Apr 03 '19 at 15:39
  • Is it an array of Strings? – dustytrash Apr 03 '19 at 15:40
  • I was trying to use same code as here: https://stackoverflow.com/questions/43312848/moment-js-isbefore-function-not-working-as-expected Also array looks like this: 1: "02/11/2019 11:22:28" 2: "05/14/2019 12:17:49" 3: "06/06/2019 09:00:00" 4: "05/02/2019 11:05:30" – NiTjSefni Apr 03 '19 at 15:45

2 Answers2

1

I don't think you need to import a JS framework to do this.

You can compare Dates in plain JavaScript.

const arrayOfDates = ["02/11/2019 11:22:28", "02/11/2020 11:22:28",
"04/03/2019 11:33:54"];

const TODAY = new Date();
const END_OF_TODAY = new Date(TODAY.getFullYear(), TODAY.getMonth(), TODAY.getDay(), 24, 59, 59);
const END_OF_TOMORROW = new Date(TODAY.getFullYear(), TODAY.getMonth(), TODAY.getDay() + 1, 24, 59, 59);

let datesBeforeToday = [];
let datesBetweenNowAndEndOfDate = [];
let datesWithTomorrowsDate = [];

for (var i=0; i<arrayOfDates.length; i++)
{
    var dateInArray = new Date(arrayOfDates[i]);
    // Before today
    if (TODAY > dateInArray)
    {
        datesBeforeToday.push(dateInArray);
    }
    // between now - end of today
    else if (END_OF_TODAY >= dateInArray)
    {
        datesBetweenNowAndEndOfDate.push(dateInArray);
    }
    // between end of today - end of tomorrow
    else if (END_OF_TOMORROW >= dateInArray)
    {
        datesWithTomorrowsDate.push(dateInArray);
    }
}

// 1) Number of dates before 'now'
console.log(datesBeforeToday.length);
// 2) Number of dates between 'now' and the end of the current date
console.log(datesBetweenNowAndEndOfDate.length);
// 3) Number of dates with tomorrow date
console.log(datesWithTomorrowsDate.length);
dustytrash
  • 1,568
  • 1
  • 10
  • 17
1

This is one way to do it without using moment.js:

var dates = [
"11/02/2018 11:22:28",
"02/02/2019 11:22:28",
"04/04/2019 11:22:28",
"10/03/2019 11:22:28",
"11/02/2019 11:22:28"
];

var res = { before: 0, tomorrow: 0, after: 0 }
var tomorrow00 = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
tomorrow00.setHours(0, 0, 0, 0);
var tomorrow24 = new Date(new Date().getTime() + 48 * 60 * 60 * 1000);
tomorrow24.setHours(0, 0, 0, 0);
tomorrow24.setTime(tomorrow24.getTime() - 1)

dates.forEach((date)=>{
    var current = new Date(date);
    if ((current >= tomorrow00) && (current <= tomorrow24))
        res.tomorrow++;
    else if (current < Date.now())
        res.before++;
    else if (current > Date.now())
        res.after++
})

console.log(res);

This is a way to solving USING moment.js

var dates = [
"11/02/2018 11:22:28",
"02/02/2019 11:22:28",
"04/04/2019 11:22:28",
"10/03/2019 11:22:28",
"11/02/2019 11:22:28"
];

var res = { before: 0, tomorrow: 0, after: 0 };
const tomorrow00 = moment().add(1, 'day').startOf('day');
const tomorrow24 = moment().add(1, 'day').endOf('day');

dates.forEach((date)=>{
    var current = moment(date, "MM/DD/YYYY HH:mm:ss");

    if ((current.isAfter(tomorrow00)) && (current.isBefore(tomorrow24)) )
        res.tomorrow++;
    else if (current.isBefore(moment().startOf('day')))
        res.before++;
    else if (current.isAfter(moment().endOf('day')))
        res.after++
})

console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73