admin管理员组文章数量:1355599
Dates list:
const datesToBeChecked = [ '2020-07-06', '2020-07-13', '2020-07-20' ]
Date to be checked for
const dateToCheckFor = '2020-07-07';
How can I get the nearest date for the date in the dates array using moment.js?
I want the function to return 2020-07-13
in this case.
I found a couple of answers online but none of them use YYYY-MM-DD format neither moment.js. I want to use moment.js to achieve this, thanks!
Dates list:
const datesToBeChecked = [ '2020-07-06', '2020-07-13', '2020-07-20' ]
Date to be checked for
const dateToCheckFor = '2020-07-07';
How can I get the nearest date for the date in the dates array using moment.js?
I want the function to return 2020-07-13
in this case.
I found a couple of answers online but none of them use YYYY-MM-DD format neither moment.js. I want to use moment.js to achieve this, thanks!
- I suggest you have a look at the countdown library for moment js, this can help you further – Sean Commented Jun 15, 2020 at 2:58
3 Answers
Reset to default 7You can use the diff method to pare dates.
const datesToBeChecked = ['2020-07-06', '2020-07-13', '2020-07-20']
const dateToCheckFor = '2020-07-07';
let nearestDate;
datesToBeChecked.forEach(date => {
let diff = moment(date).diff(moment(dateToCheckFor), 'days');
if (diff > 0) {
if (nearestDate) {
if (moment(date).diff(moment(nearestDate), 'days') < 0) {
nearestDate = date;
}
} else {
nearestDate = date;
}
}
});
console.log(nearestDate);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.26.0/moment.min.js"></script>
In my case, I want to find the nearest value. In case, it can not find a larger value in the array then it should return the largest date from the array. Also, Dates Should be verified.
const findNearestDate = (arrayOfDates, findDate) => {
try {
let nearestDate, momentsDate;
if (moment(findDate).isValid()) {
momentsDate = arrayOfDates.map(date => {
if (moment(date).isValid()) {
date = moment(date);
const diff = moment(date).diff(moment(findDate), 'seconds');
if (diff >= 0) {
if (nearestDate) {
if (moment(date).diff(moment(nearestDate), 'seconds') < 0) {
nearestDate = date;
}
} else {
nearestDate = date;
}
}
} else {
date = false;
}
return date;
}).filter(isValid => isValid);
}
if (!nearestDate) {
nearestDate = moment.max(momentsDate);
}
return nearestDate.format('YYYY-MM-DD HH:mm:ss');
} catch (error) {
console.error(`Error In findNearestDate ${error}`);
return false;
}
};
const datesToBeChecked = ['1998-03-16 10:15:00', '1998-03-16 10:15:10', '1998-03-16 09:00:00', '1998-03-16 10:00:00', '1998-03-16 10:00:00', '1998-03-16 10:20:00', '1998-03-16 110:20:00'];
// Remove Below Line if you want to see the warnings
console.warn = () => {};
document.write('1998-03-16 10:15:00 ' + '=>' + findNearestDate(datesToBeChecked, '1998-03-16 10:15:00') + '<br/>');
document.write('1998-03-16 10:18:00 ' + '=>' + findNearestDate(datesToBeChecked, '1998-03-16 10:18:00') + '<br/>');
document.write('1998-03-16 10:23:00 ' + '=>' + findNearestDate(datesToBeChecked, '1998-03-16 21:23:00') + '<br/>');
document.write('1998-03-16 210:23:00 ' + '=>' + findNearestDate(datesToBeChecked, '1998-03-16 210:23:00') + '<br/>');
document.write('1998-03-16 09:23:00 ' + '=>' + findNearestDate(datesToBeChecked, '1998-03-16 09:23:00') + '<br/>');
document.write('1998-03-16 10:20:00 ' + '=>' + findNearestDate(datesToBeChecked, '1998-03-16 10:20:00') + '<br/>');
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.26.0/moment.min.js"></script>
This assumes you are okay with closest date before (could be changed to after) your date:
function get_closest_date(dateToCheckFor){
var arr = $.map( datesToBeChecked, function (a) {
return [moment(a).isSameOrBefore(dateToCheckFor) ? a : null ]
})
return arr.find(el => el !== null) // return first valid value
}
本文标签: javascriptHow can I get the nearest date through momentjsStack Overflow
版权声明:本文标题:javascript - How can I get the nearest date through moment.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744009921a2575373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论