0

I need to know if the date is equal to or less than today.

  let currentTimeInSeconds = new Date().getTime() / 1000;
  currentTimeInSeconds = currentTimeInSeconds.toString().substring(0, currentTimeInSeconds.toString().indexOf('.'));
  this.UnpaidDuebills = this.UnpaidDuebills.filter(bills => bills.expirationDate <= currentTimeInSeconds);

bills.expirationDate returns a date such as 22/01/2018 in string format.

I don't know how to transform that string format in seconds inside that array filter.

3 Answers 3

1

Firstly, you should get year, month, day from your expirationDate string first. And you can use create date type based on the expirationDate.

and transfer to Number(millisecond) using getTime(). And then compare to filter.

define getNewDate function

function getNewDate(expirationDate){
    const year = getYear(expirationDate);
    const month = getMonth(expirationDate);
    const day = getDay(expirationDate);
    return newExpirationDate = new Date(year, month, day)
}

and filter using that function

this.UnpaidDuebills = this.UnpaidDuebills.filter(bills => getNewDate(bills.expirationDate).getTIme() <= currentTimeInSeconds);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the Date Object Method called UTC(). More details about the function

Basically, you just need to do :

Date.UTC(year, month, day)

Given that, your variable is bills.expirationDate in the format dd/mm/yyyy, you need to split it.

You can do like that:

this.UnpaidDuebills = this.UnpaidDuebills.filter(bills => Date.UTC(bills.expirationDate.split("/")[2], bills.expirationDate.split("/")[1], bills.expirationDate.split("/")[0]) <= currentTimeInSeconds);

Comments

0

In order to do this filter you need to put the limit of the date, it has to be midnight or just to be sure one second before midnight for the expirationDate.

let currentTimeInSeconds = new Date().getTime() / 1000;
currentTimeInSeconds = currentTimeInSeconds.toString().substring(0, currentTimeInSeconds.toString().indexOf('.'));
this.UnpaidDuebills = this.UnpaidDuebills.filter(bills => {
moment(bills.expirationDate + ' 23:59:59','DD/MM/YYYY hh:mm:ss').valueOf() <= currentTimeInSeconds});

I like to use moment.js to parse the dates, but is up to you, You just need to set the hour to take the entire day and do an acurate comparision.

Cheers!

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.