score:1
Use Math.max
to return the larger of two values. In your case, you want to return the number of days remaining only if that number is greater than 0, otherwise you want to return 0:
const counters = this.state.counters.map((counters, i) => {
let untildate = counters.date
let diffDays1=(function(){
let oneDay = 24*60*60*1000 // hours*minutes*seconds*milliseconds
let secondDate = new Date(untildate);
let firstDate = new Date();
return (`${Math.max(0, Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))))} Days`)
})();
score:0
There are many answers for how to get the difference between two dates in days, like this.
Your code seems overly convoluted. Assuming the input dates are in a format like 2019-05-10 and you're using the built-in parser, you can work in UTC and avoid any daylight saving issues.
Just test the difference in days, if it's 0 or less, return zero. Otherwise, return the difference. E.g.
var dates = ['2019-01-01','2019-05-10','2019-05-20'];
var counters = dates.map(date => {
let diffDays = (new Date(date) - new Date().setUTCHours(0,0,0,0))/8.64e7;
return diffDays > 0? diffDays : 0;
});
console.log(counters)
Source: stackoverflow.com
Related Query
- How to get days until X date and stop counting when X date reaches 0
- How do I get my HTML form date input to only allow specific days of the week to be chosen using JavaScript, React and HTML?
- Calculate how many days until an event, based on a recurring date and recurrence
- In Firebase when using push() How do I get the unique ID and store in my database
- How to get the first and last visible date in React Big Calendar?
- How do I properly get the date and time?
- how to disable all the Sundays and array of specific days in ant design date picker
- How to get date from Date Picker and show in the table? Material-UI. ReactJS
- How to get line coverage on React.useEffect hook when using Jest and enzyme?
- How to Upload Images to Firebase Storage and Get Image's Download URL's When Using Dropzone in React.js
- Reactjs How to get the value of Start date and End date?
- how to I get a user's IP address when separate client and server apps, in Node with Nest.js
- How do I set up WordPress and WooCommerce API so that I won't get CORS error when posting?
- How to stop screen scrolling when page navigation links are clicked, React-Bootstrap and Gatsby
- How can i stop redux action? when i get error
- ReactJS: How to get response array inside State(useState) and check the length of the array when rendering/binding
- How to stop redux-form or React from changing htmlFor and id when creating Jest snapshots?
- How to get start and end date value in React date range picker?
- How to get id and input data when onChange event is happening React js?
- How to change a <a> tag color when I click on it and it doesn't change until I change my URL
- How to get the month and year from the past date untill now JavaScript
- How to format get and format Date in DatePicker in Material UI React
- How to pass variable to graphql from method when onClick with button and get it's return data back?
- How can I make this interval stop when it reaches the end of the array?
- How to refresh the table data when one of the item has its valid till date and time has expired in React Js
- React Loader - Trying to get a loader when api request is made and to stop it when response is fetched
- How to get the ref.current dom element when using react-select and react refs?
- How do I get my React like button to increment when not liked, and then decrement when liked?
- How to get and modify the Date value
- How to get two or more components to listen to eachother and turn off when others turn on
More Query from same tag
- Is there way to set default value to prop which is child of required prop?
- Passing values through React-Router v4 <Link />
- Need to render Routes component only once while keeping conditional rendering of layout
- How to use useState hook with array state for children that call the setter function without infinite rendering loop?
- Unable to find type definitions when using typescript 3.0.1 project refferences
- How to change tab from parent in react material Tabs
- React - my map function on my array component is not defined when I try to declare it somewhere else
- NextJS- How handle redirect, meanwhile my strategy returns an error
- Use default Material-UI validation in form submit
- Unique "key" prop not working for my array mapping - REACT
- Why is my game not failing if the same card is clicked twice?
- React modal reusing
- Redirection with reactjs and nodejs is not working
- React useState entering infinite loop
- Close React Modal With Submit
- NextJS scroll into view of specific component upon button OnClick that exists on a different js file
- SyntaxError: Unexpected token import in node_modules
- Argument of type 'never[]' is not assignable to parameter of type 'never' when declaring a type in Typescript
- React: Reaching Nested State Items w/ Reducer
- Testing react-hotkeys using react-testing-library
- React Component keeps on Rendering
- Is passing the "this" context through props an anti-pattern?
- Updating multiple attributes of a nested state array of objects from a dynamic object
- Multiple Calls to Custom Hook not yielding expected result
- GraphQL Unexpected end of JSON input
- Sending POST request from localhost to heroku server?
- Why my Array find function doesn't work properly even I did the same thing document
- How to stop form value resetting when using ProtectedRoutes in react js
- Reason behind using Observables when dealing with AJAX requests
- React + Firebase v3 - how to get a 2nd reference to firebase to update a single item