Reputation: 549
I'm currently tying to compare 2 dates (and time): the date limit my action has to be completed and the current date.
I receive the date and time in this format: 2017-05-29T15:30:17.983Z
.
I then try to compare the 2 dates using this function:
function checkExceedLimit (props) {
exceedLimit = 0;
props.items.map((item) => {
var dateLimit = moment(item.limit)
var now = moment()
if (item.limit != null || item != ' ' && now > dateLimit) {
exceedLimit++;
console.log(now)
console.log(dateLimit)
}
})
}
Basically I want to compare the limit
from each item and add +1 to exceedLimit when the current date is passed. Yet it returns +1 for each limit even though not all of them are passed.
Thank you for your help
Upvotes: 2
Views: 35490
Reputation: 1807
First of all you should proper create your instance of moment.
Like this:
moment("2017-05-29T15:30:17.983Z", 'YYYY-MM-DDTHH:mm:ss.SSSZ')
You can check this link for more details: https://momentjs.com/docs/#/parsing/string-format/
And then you can compare date using the moment's API methods on https://momentjs.com/docs/#/query/
So your code should look like this:
function checkExceedLimit (props) {
exceedLimit = 0;
props.items.map((item) => {
const dateLimit = moment(item.limit, 'YYYY-MM-DDTHH:mm:ss.SSSZ');
const now = moment()
if (dateLimit.isValid() && now.isAfter(dateLimit)) {
exceedLimit++;
console.log(now.toString())
console.log(dateLimit.toString())
}
})
}
Upvotes: 3