I need the closest image of a collection to a date. Let's say the image that is closest to 1st of August 2019.

The help says that

Creates a unary or binary filter that passes if the left operand, a date range, contains the right operand, a date.

which seems to be what I need. I expect to use it like this


but the problem is that dateRangeContains has the following arguments:

ee.Filter.dateRangeContains(leftField, rightValue, rightField, leftValue)
leftField (String, default: null):

A selector for the left operand. Should not be specified if leftValue is specified.

rightValue (Object, default: null):

The value of the right operand. Should not be specified if rightField is specified.

rightField (String, default: null):

A selector for the right operand. Should not be specified if rightValue is specified.

leftValue (Object, default: null):

The value of the left operand. Should not be specified if leftField is specified.

Considering the definition, I did left the first 2 arguments aside, and used only the last 2, which are named in the definition, i.e. the left operand, a date range, contains the right operand, a date:


Needless to say, it didn't work, although there is an image with the date on 2019-07-25


I think there is some confusion here regarding what dateRangeContains does. It DOES NOT give you the image closest to the date you want. It just checks a DateRange object in the metadata of an image and gives you images which contains the date you supplied within the range listed in its metadata. The string field is actually supposed to be the name of the DateRange field and the object field is supposed to be the date that you want. for example,

var ic = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')


// since ls8 images only have start date, i am just advancing the
// date by 1 month for demonstration purpose
ic = ic.map(function(image){
  return image.set('daterange',ee.DateRange(ee.Date(image.get('system:time_start')),ee.Date(image.get('system:time_start')).advance(1,'month')))

ic = ic.filter(
print(ic.size(), ic.first());

So, it does not do what you want. However, you can do it by using the system:time_start property of the images and compute how far they are from the date you want. Once you sort it using that you are good to go.

// narrow down images to some smaller window to look within smaller dataset 
var ic = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')

// convert required date to millis
var requiredDate = ee.Date('2019-08-01').millis();

// calculate difference between image date and required  date. store the absolute value
ic = ic.map(function(image){
  return image.set('dateDist', ee.Number(image.get("system:time_start")).subtract(requiredDate).abs());

// sort the image using that absolute difference and take the first one 
var closestImage = ic.sort('dateDist').first();

// check the date of closest available image

Although, if you want multiple scenes, you might need to take a set of images than just first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.