Determine the difference between dates using Angular 2's array filtering function

Here's a way to get the current time in seconds:

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

I have an array containing objects with an expirationDate property that returns expiration time in seconds. I can find the bills that have expired, but I want to exclude the ones expiring today.

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

The code above isn't effective because the current time is always changing due to hours, minutes, seconds, and milliseconds.

Is there a way to compare expirationDate to today without considering the exact time?

Edit: Here is an example of a bill

name: Test bill

paid: false

expirationDate: 1535598000 (equivalent to August 30, 2018, 3:00:00 AM)

amount: $231.33

Answer №1

What do you think about this solution?

checkExpiration(expirationDate) {
  const startDay = new Date();
  startDay.setHours(0, 0, 0, 0);

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

  const expiryTime = expirationDate * 1000;

  return (expiryTime > startDay && expiryTime < endDay);
}

this.ExpiringTodayItems = this.UnpaidBills
  .filter(items => !items.paid && this.checkExpiration(items.expirationDate));

You can verify this by accessing the Browser Dev Tools, navigating to the console, and executing the following:

function isCurrentDay(expiryTime) {
    const startDay = new Date();
    startDay.setHours(0, 0, 0, 0);

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

    return (expiryTime > startDay && expiryTime < endDay);
}

var currentDate = new Date();
isCurrentDay(currentDate.getTime())  // true

Answer №2

To efficiently manage your data, it is recommended to iterate through your dataset and transform the expirationDate from currentTimeInSeconds to a readable date. Utilizing the library moment.js can simplify this process. After conversion, you can execute the following:

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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I adjust the appearance of an HTML tag within an Angular component?

I have created an Angular component with the following definition: import { Component, Input, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'rdc-dynamic-icon-button', templateUrl: './dynamic-icon-button. ...

Ways to invoke a prop function from the setup method in Vue 3

I encountered the following code: HTML: <p @click="changeForm">Iniciar sesion</p> JS export default { name: "Register", props: { changeForm: Function, }, setup() { //How do I invoke the props change ...

Issue with esbuild not executing within docker compose configuration

Currently, I am new to using esbuild and struggling to set up a script that can watch and rebuild my files. Additionally, I need this functionality to work within a docker environment. Within my package.json file, the following scripts are defined: " ...

What is the method by which the Material-UI Button component determines the properties for the component that is passed to the `component` prop

Could someone please clarify how Material-UI enhances the properties of its Button component by incorporating the properties of a specific component if passed in the component attribute? interface MyLinkProps extends ButtonBaseProps { someRandomProp: str ...

The object literal's property 'children' is assumed to have a type of 'any[]' by default

Is there a way to assign the property myOtherKey with any value? I encountered a Typescript error that says.. A problem occurred while initializing an object. The property 'children' in the object literal implicitly has an array type of 'a ...

Unexpected Quote Will Not Appear

My random quote generator is not functioning properly, it should display a different quote on each click of the button. My colleagues are also facing the same issue. It was working fine when implemented in JavaScript, but after converting all the syntax to ...

What is the best way to add a component into a MAP of other components in a React application?

I have a project where I am implementing a MAP function to iterate through an array of objects and present the data using a special Card component. However, I want to include another component called Banner after every second Card element from the loop, an ...

Leveraging IntersectionObserver to identify the video in view on the screen

Our Objective I aim to implement a swipe functionality for videos where the URL changes dynamically based on the ID of the currently displayed video. Challenges Faced Although I managed to achieve this with code, there is an issue where the screen flashe ...

Saving Information from Various MongoDB Searches on Node.js

Okay, I believe it would be more helpful if I provide my entire node.js route for you to better understand what I am trying to explain. The issue I am facing is with making multiple queries to my MongoDB database (one for each day). The queries are execut ...

I am searching for answers to solve issues related to a JSON file

I am currently working on a tool that searches for matches in an input field by comparing the keywords entered by the user with a JSON. During my testing phase, I focused on using a single API that provides information about different countries and fortun ...

Here is a way to attach a function to dynamically generated HTML that is returned in an AJAX request

As I was working on a new function development project, I encountered a situation where I had to add a function to dynamically generated HTML through an ajax call. The following is a snippet of the code: $.ajax( 'success': function(data){ ...

Is there a different option instead of using the exit() function?

I am attempting to modify the class of a button based on a condition not being met (if(strlen($username) < 5)). Here is my code: if( strlen($username) < 5 ){ echo '<span class = "glyphicon glyphicon-remove" style = "color:red"> ...

"Unraveling the layers: Mastering nested API calls in

Is there a more efficient method to accomplish this task of retrieving all users along with their photos: this.authHttp.get(this.ApiUrl+'users') .map(res => res.json()) .subscribe(users => { for (let user of users) { ...

Ordering in D3 Code

I'm struggling with understanding the correct structure for writing D3 code. For example: In the code snippet below, I noticed that if I place the 3rd section (Chart Title) of the code after creating the svg element, the title text doesn't displ ...

Using jQuery to loop through a table and retrieve the button value from every row

I have a challenge with dynamically created buttons in a table. My goal is to loop through the table, identify the rows that have a checked checkbox, and retrieve the value of a button within that row. I then need to store these values in an array. Unfortu ...

Ways to verify the functionality of a function utilizing a Subscription that yields no return value

I'm working with a TypeScript ModelService that has an edit function: edit(model: Model): Observable<Model> { const body = JSON.stringify(model); return this.http.put(`/models/edit/${model.id}`, body)); } Additionally, there is a TypeScrip ...

Error encountered while trying to display the react-bootstrap table

I am having trouble rendering sample data using react-bootstrap tables. Every time I try, I encounter the error: TypeError: Cannot read property 'filter' of undefined I've searched on various platforms and visited multiple links, but I ca ...

What is the best way to establish a restriction on the number of items obtained from an RSS

I am receiving a feed from this specific link. My goal is to only retrieve the initial five items from the feed, but currently I am getting all of the items. Is there a way for me to specifically request only the first five items? Do I need to include an ...

Adding to an existing array in SQLite by updating a column using Sequelize

My code includes a model definition for saving product data using Sequelize: This is how the Product model looks: import {Optional, Model, Sequelize, DataTypes } from 'sequelize'; /*This is the Product model used to save the data about products* ...

Utilizing bcrypt for enhanced security in Angular 7

Can anyone assist me with using bcrypt in Angular7 to securely store an encrypted password in MySQL? I have successfully installed bcrypt using npm install bcrypt and imported it as import * as bcrypt from 'bcrypt';. While the code compiles wit ...