google-earth-engine – How to Change Date Format into Readable Format in GEE

dateformatgoogle-earth-enginetable

I have code in GEE which create table with the sentinel2 mean NDVI data for certain period.
my problem is that the table in the end has two column with the dates, one is the name of each image (not easy to read- 20191004T051651_20191004T052839_T43QGC_00000000000000000000 for example) and the other has also dates but not readable ( for example : 1573623201083)

I have tried to change the format in different places in my code so it will get the correct time values, e.g:

// Collect region, image, value triplets.
var triplets = filter.map(function(image) {
  return image.select('NDVI').reduceRegions({
    collection: geometry.select(['Name']), 
    reducer: ee.Reducer.mean(), 
    scale: 20
  }).filter(ee.Filter.neq('mean', null))
    .map(function(f) { 
      return f.set('time', image.get(ee.Date('system:time_start')));
    });
}).flatten();

print('triplets', triplets);

FeatureCollection (Error) Date: Bad date/time 'system:time_start'

and:

      // Get the list of all features with a unique row ID.
      var values = ee.List(row.get('matches'))
        // Map a function over the list of rows to return a list of
        // column ID and value.
        .map(function(feature) {
          feature = ee.Feature(feature);
          return [feature.get(Name), feature.get('mean')];
        });
      // Return the row with its ID property and properties for
      // all matching columns IDs storing the output of the reducer.
      // The Dictionary constructor is using a list of key, value pairs.
      return row.select(ee.Date([time])).set(ee.Dictionary(values.flatten()));
    });
};

Invalid argument specified for ee.Date(): time

This is link to the script:
https://code.earthengine.google.com/f4bb8bcac7d2e351523b33d9401a8618

it has already the geometry , in case it doesn't work let me know

My end goal: to have readable date column in my table

Best Answer

Earth Engine handles time for their assets in Unix Epoch Time (Miliseconds since 1970). This is the format you get from image.get("system:time_start"). For filtering and other functions thats also the format which has to be passed to system:time_start.

Here's a bit of sample code to show how you can convert between date formats:

https://code.earthengine.google.com/30a20bcc18bf0cd267dead6d31e8c7df

// Random dummy Image
var image = ee.Image("COPERNICUS/S2_SR/20200401T001611_20200401T001605_T01WCR");

// system:time_start is saved as miliseconds (epoch):
var epoch = image.get("system:time_start")
print("Epoch system:time_start:",epoch)

// This can be converted to a readable date object by passing it to ee.Date
var readableDate = ee.Date(epoch)
print("Epoch Time as ee.Date Object:", readableDate)

// To get from an Date object to a string, use .format() and the format you want to use
var stringDate = readableDate.format("YYYY-MM-dd")
print("ee.Date object to string", stringDate)

// To get back to the epoch timestamp (miliseconds) use .millis() on an ee.Date object
// This is absolutely necessary if you want to put a new date into system:time_start
var toMillis = readableDate.millis()
print("Back from ee.Date to Epoch", toMillis)
Related Question