Lookup Values Corresponding to the Closest Date

Lookup values corresponding to the closest date

sapply(y, function(z) x$Value[which.min(abs(x$date - z))])
# [1] 40 65 44 67 44

Find closest date from array of objects to given date

If you need to find the closest date to now, you should reduce your items.

While reducing, you should know how to access the date field and how to parse it. If the distance between the current (curr) and the target it less than the previous (prev), update the accumulator (prev) to be the current (curr).

const findClosest = (data, accessor, target = Date.now()) =>
data.reduce((prev, curr) => {
const a = Math.abs(accessor(curr).getTime() - target);
const b = Math.abs(accessor(prev).getTime() - target);
return a - b < 0 ? curr : prev;
});

const sampleData = [
{ "name": "A", "date": "31/12/2000" },
{ "name": "B", "date": "31/12/2010" },
{ "name": "C", "date": "31/12/2100" }
];

const processDateString = (dateString) => {
const [ date, month, year ] = dateString.split(/\//g).map(Number);
return new Date(year, month - 1, date);
};

const closest = findClosest(sampleData, ({ date }) => processDateString(date));

console.log(closest);

Find the closest date to a given date

This function will return the datetime in items which is the closest to the date pivot.

def nearest(items, pivot):
return min(items, key=lambda x: abs(x - pivot))

The good part this function works on types other than datetime too out of the box, if the type supports comparison, subtraction and abs, e.g.: numbers and vector types.

Find closest past date in list of dates

Using min with two keys would be one way:

from datetime import datetime

now = datetime.now()
min(datelist, key=lambda x: (x>now, abs(x-now)) )

Output:

datetime.datetime(2020, 1, 31, 0, 0)

How do I find the closest date to a given date?

Following through on Henrik's suggestion to use findInterval. We can do:

library(zoo)
interval.idx <- findInterval(index(dates.zoo), index(monthly.zoo))
interval.idx <- ifelse(interval.idx == 0, NA, interval.idx)
dates.zoo$month <- index(monthly.zoo)[interval.idx]

Find closest date in SQL Server

The where clause will match all rows with date less than @CurrentDate and, since they are ordered descendantly, the TOP 1 will be the closest date to the current date.

SELECT TOP 1 *
FROM x
WHERE x.date < @CurrentDate
ORDER BY x.date DESC

Closest date in a vector to a given date

you miss an abs to take care of negative values:

which(abs(coldate-x) == min(abs(coldate - x)))
[1] 4

Index Match with multiple criteria needs to return the closest value that is less than or equal to search value

=AGGREGATE(14,6,($AO$2:$AO$5000)/(($AN$2:$AN$5000=A2)*(($AO$2:$AO$5000)<=F2)),1)

the part after the / will evaluate to 0 if either is false. this causes an error which aggregate will ignore due to the 6. 14 tells aggregate to sort the results from largest to smallest. 1 tells aggregate to return the 1st largest value that is less than or equal to F2.

Find closest date in one query

check this one

db.collection.find({"time":{$gte: isoDate,$lt: isoDate}}).sort({"time":1}).limit(1)

Please use the same format what mongodb support like following

ISODate("2015-10-26T00:00:00.000Z")


Related Topics



Leave a reply



Submit