3

I'm using GEE and I have many codes were I filter my imagecollection by using filterdate (and then I have date range) and then I work with the first image in the imagecollection, when I know from the beginning which date I want . I want to change my code so I can choose one specific date.

for example, if I want to get image from march 15th, my current code does this:

  var dataset = ee.ImageCollection('COPERNICUS/S2')
                    .filterDate('2020-03-14','2020-03-16')
                    // Pre-filter to get less cloudy granules.
                    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                    .select('B1','B2','B3','B4','B8','QA60')
                    .filterBounds(geometry)
                    .map(maskS2clouds);

  });

instead of filterDate with two dates, I would love to have something like that:

  var dataset = ee.ImageCollection('COPERNICUS/S2')
                    .filterDate('2020-03-15')
                    // Pre-filter to get less cloudy granules.
                    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                    .select('B1','B2','B3','B4','B8','QA60')
                    .filterBounds(geometry)
                    .map(maskS2clouds);

  });

But this doesn't work. I remembered in the past I mange to do this but can't find the script and also in the internet I find only date-range filter.

My end goal: to be able to insert one specific date.

2 Answers 2

3

This can be achieved with ee.Date.advance() and first defining your day of interest in a separate variable.

Here's with advance:

var dayOfInterest = ee.Date('2020-03-15')

var dataset = ee.ImageCollection('COPERNICUS/S2')
                    .filterDate(dayOfInterest, dayOfInterest.advance(1, 'day'))
                    // Pre-filter to get less cloudy granules.
                    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                    .select('B1','B2','B3','B4','B8','QA60')
                    .filterBounds(geometry)
                    .map(maskS2clouds);
3

This still works, but it probably doesn't do what you want it to. From the ee.ImageCollection.filterDate() docs:

end (Date|Number|String, optional):

The end date (exclusive). Optional. If not specified, a 1-millisecond range starting at 'start' is created.

If you have an exact date (with time), you can filter so you only get images from that date/time (within 1 ms).

https://code.earthengine.google.com/0f6e184a365b71fc554fdee92bea70d2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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