Google Earth Engine – How to Filter Image Collection to One Specific Date

datefiltergoogle-earth-engine

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.

Best Answer

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);