0

This will get me current time in seconds:

let currentTimeInSeconds = new Date().getTime() / 1000;

I have an array with the object expirationDate, it returns that information in seconds. I can get the bills that expired, however, I'd like to filter out the bills that are expiring today.

this.ExpiringTodayBills = this.UnpaidDuebills.filter(bills => bills.paid === false && bills.expirationDate = currentTimeInSeconds);

The above code doesn't work because current time changes constantly due to hours, minute, seconds and milliseconds.

How would I be able to compare if expirationDate = today, regardless of the time?

Edit: This is an example of a bill

name: Test bill

paid: false

expirationDate: 1535598000 (which means August 30, 2018 3:00:00 AM)

amount: 231.33

Rosenberg
  • 2,424
  • 5
  • 33
  • 56

2 Answers2

3

How about this?

isExpiringToday(expirationDate) {
  const start = new Date();
  start.setHours(0, 0, 0, 0);

  const end = new Date();
  end.setHours(23, 59, 59, 999);

  const expiryDateTimeStamp = expirationDate * 1000;

  return (expiryDateTimeStamp > start && expiryDateTimeStamp < end);
}

this.ExpiringTodayBills = this.UnpaidDuebills
  .filter(bills => !bills.paid && this.isExpiringToday(bills.expirationDate));

You can test this by opening the Browser Dev Tools, going to the console and doing this:

function isToday(expiryDateTimeStamp) {
    const start = new Date();
    start.setHours(0, 0, 0, 0);

    const end = new Date();
    end.setHours(23, 59, 59, 999);

    return (expiryDateTimeStamp > start && expiryDateTimeStamp < end);
}

var newDate = new Date();
isToday(newDate.getTime())  // true
SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
0

You should loop through your data and convert your expirationDate from currentTimeInSeconds to a date. The library moment.js can be helpful. Next, you can do:

this.ExpiringTodayBills = this.UnpaidDuebills.filter(bills => bills.paid === false && bills.expirationDate = today);

const today = moment().format('MM/DD/YYYY');
console.log(today);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
Damien
  • 1,582
  • 1
  • 13
  • 24