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

5
  • Have you tried this: stackoverflow.com/questions/8305259/… Commented Sep 3, 2018 at 19:55
  • @SiddharthAjmera and how would I be able to do that inside a filter? Granted that I can do that with currentTimeInSeconds, but what about bills.expirationDate that is coming out from inside the filter? Commented Sep 3, 2018 at 19:56
  • Do you get a timestamp on bill.expirationDate ? Commented Sep 3, 2018 at 19:58
  • @SiddharthAjmera nope, regular current time in seconds. Commented Sep 3, 2018 at 19:58
  • Can you please post some bills in the question? Commented Sep 3, 2018 at 19:59

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

How would it know if the bill expires today? It gets all the expired bills, but I wanna cherry pick the ones that expire today.
Updated the answer.
I feel like this is very close to the answer, but unfortunately it returns false to every date, including the ones expiring today.
Works for me. I swear :P
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.