我一直在尝试在给定日期的数组中搜索最近的即将到来日期
这是阵列:
[
'September 01',
'September 06',
'September 08',
'September 13',
'September 15',
'September 20',
'September 22',
'September 27',
'September 29',
'October 04',
'October 06',
'October 11',
'October 13'
]
例如,今天的日期是"9月07日">,我希望输出是'9月08日'
日期的格式不能更改
如果有可能使用这些日期格式使其适用于任何给定的数组
我尝试了以下方法:
let date = new Date();
let closestDay;
const Dates = [
'September 01',
'September 06',
'September 08',
'September 13',
'September 15',
'September 20',
'September 22',
'September 27',
'September 29',
'October 04',
'October 06',
'October 11',
'October 13'
]
var currentDay = date.getDate();
const currentMonth = date.getMonth();
var currentDate = currentMonth + " " + (currentDay<10)? "0" + currentDay : currentDay;
const getClosestDay = ( Dates, Date) => {
while(!closestDay){
for( let i=0; i<Dates.length; i++){
if(Date == Dates[i]){
closestDate = Dates[i];
break;
}
}
if(!closestDate){
currentDay++;
currentDate = currentMonth + " " + (currentDay<10)? "0" + currentDay : currentDay;
}
}}
但这一进展在月底造成了一个逻辑错误,因为不会返回日期-例如-9月29日
看起来你想要的是下一个日期,而不是"最近的";日期例如,我将假设一个输入"0";9月02日";应返回";9月6日";。还有几步要走,所以让我们开始吧。
首先,获取原始数组,并使用Date
获取自1970年1月1日00:00:00 UTC以来的毫秒数。如果您不太使用Javascript的Date
,这可能看起来很奇怪,但这正是许多日期计算所使用的。你的例子中没有给出年份,所以我假设是今年,或者你的搜索没有多大意义。
const DateHaystack = ['September 01', 'September 06', 'September 08', 'September 13', 'September 15', 'September 20', 'September 22', 'September 27', 'September 29', 'October 04', 'October 06', 'October 11', 'October 13', 'October 18', 'October 20', 'October 25', 'October 27', 'November 01', 'November 03', 'November 08', 'November 10', 'November 15', 'November 17', 'November 22', 'November 24', 'November 29', 'December 01', 'December 06', 'December 08'];
const currentYear = new Date().getFullYear();
const DateInMs = DateHaystack.map(DateString => Date.parse(DateString + ", " + currentYear))
现在,我们今天(或您使用的任何日期作为搜索点(都需要相同的
const Today = Date.parse(new Date())
好的,我们知道你所有的日期是什么时候在数组中的,我们也知道今天是什么时候,以1970年的ms为单位。接下来,筛选所有小于今天的日期。根据结果,找到最小值。那将是下一天。
const UpcomingDayInMs = Math.min(...DateInMs.filter(DateNumber => Today < DateNumber));
最后,将其解析为日期,并提取月份和日期。
const Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const UpcomingDay = new Date(UpcomingDayInMs)
const UpcomingDayString = `${Months[UpcomingDay.getMonth()]} ${UpcomingDay.getDate()}`;
UpcomingDayString
现在应该包含您想要的字符串。
您可以将字符串转换为日期,然后进行比较,然后以字符串形式再次发送结果,如下所示
const inputDatesArray = ['September 01', 'September 06', 'September 08', 'September 13', 'September 15', 'September 20', 'September 22', 'September 27', 'September 29', 'October 04', 'October 06', 'October 11', 'October 13', 'October 18', 'October 20', 'October 25', 'October 27', 'November 01', 'November 03', 'November 08', 'November 10', 'November 15', 'November 17', 'November 22', 'November 24', 'November 29', 'December 01', 'December 06', 'December 08']
function getNearestDate(inputDate, datesArray){
const dates = datesArray.map(dateString => new Date(dateString));
const currentDate = new Date(inputDate)
let nearestDateIndex = 0;
let minDaysBetweenDates = Infinity;
dates.forEach((date, index) => {
const diffTime = Math.abs(date - currentDate);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if(diffDays <= minDaysBetweenDates){
nearestDateIndex = index;
minDaysBetweenDates = diffDays
}
});
return datesArray[nearestDateIndex];
}
console.log(getNearestDate('September 07', inputDatesArray));
此代码返回今天之后最近的日期
这将检查数组中的每个日期,如果未找到,则添加1天
最终返回最近的日期。
例如,今天的日期是"9月07日",输出是"9月份08日">
如果您希望当天也包括在内,则设置var plus = 0
var dates = [
'September 01',
'September 06',
'September 08',
'September 13',
'September 15',
'September 20',
'September 22',
'September 27',
'September 29',
'October 04',
'October 06',
'October 11',
'October 13'
]
function getDate(){
var plus = 1;
var searching = true;
while(searching)
{
var date = new Date(); // today
date.setDate(date.getDate() + plus);
var month = date.toLocaleString('default', { month: 'long' });
var day = date.getDate();
if(month <= 9)
{
month = '0'+month;
}
if(day <= 9)
{
day = '0'+day;
}
if(dates.includes(month+ " "+day))
{
searching = false;
return month+ " "+day;
}
else
{
plus++;
}
}
}
console.log(getDate());
您可以计算日期之间的绝对差异,并取较小的一个:
const dates = ['September 01', 'September 06', 'September 08', 'September 13', 'September 15', 'September 20', 'September 22', 'September 27', 'September 29', 'October 04', 'October 06', 'October 11', 'October 13', 'October 18', 'October 20', 'October 25', 'October 27', 'November 01', 'November 03', 'November 08', 'November 10', 'November 15', 'November 17', 'November 22', 'November 24', 'November 29', 'December 01', 'December 06', 'December 08'];
const mdToday = new Date().toLocaleDateString("en-US", { month: 'long', day:'2-digit' }); // 'September 07'
const today = new Date(mdToday); // Fri Sep 07 2001 00:00:00 GMT+0500 (GMT+05:00)
const result = dates.reduce((acc, mdDate) => {
const date = new Date(mdDate);
const diff = Math.abs(today - date);
if (diff < acc.diff) {
acc.date = mdDate;
acc.diff = diff;
}
return acc;
}, { date: '', diff: Infinity })
.date;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }
这是我针对关联数组的解决方案,非常简单的
const datesArray = [
{_id: '649d4c6ad11b4694390e29d1', code: '279C1', createdAt: 1688030314437, dateTo: 1689998400000, dateFrom: 1689825600000, …}
{_id: '649967786a4d780012430b87', code: 'C2DC6', createdAt: 1687775096281, dateTo: 1689811200000, dateFrom: 1689638400000, …}
{_id: '649967266a4d780012430b70', code: 'AEE9D', createdAt: 1687775014575, dateTo: 1689552000000, dateFrom: 1689379200000, …}
{_id: '649966826a4d780012430b53', code: '86C24', createdAt: 1687774850109, dateTo: 1689292800000, dateFrom: 1689120000000, …}
{_id: '64957526f70cee4f10b64827', code: '1966D', createdAt: 1687516454889, dateTo: 1764133200000, dateFrom: 1763960400000, …}
];
const maxDate = new Date(Math.max(...datesArray.map(o => o.dateTo)));
const minDate = new Date(Math.min(...datesArray.map(o => o.dateFrom)));