Google Earth Engine – Adding Date Labels to Sentinel-2 Image Collections

datetimegoogle-earth-enginethumbnailtime

I found a way to add text to the images of a collection using a package that GEE user Gena created for this purpose. I used this code as guide, in the addText function I used the property system:time_start, but it is not working because I get one number (not the date) for the whole collection. You can find an extract of my code below and the complete code on this link

// Simple ImageCollection preview via animated GIF.

Map.addLayer(rectangle);
Map.centerObject(rectangle, 3);

// Collection
var collection = CLOROFILA_IC.select('CLOROFILA-A');
  

// Add the first image to the map, just as a preview.
var im = ee.Image(collection.first());
Map.addLayer(im, {}, "first image");

// Visualization parameters.
var args = {
  crs: 'EPSG:5367',  // CRTM05
  dimensions: '1000',
  region: rectangle,
  framesPerSecond: 1
};


var text = require('users/gena/packages:text'); // Import gena's package which allows text overlay on image

var annotations = [
  {position: 'right', offset: '1%', margin: '1%', property: 'label', scale: 100} //large scale because image if of the whole world. Use smaller scale otherwise
  ]
  
function addText(image){
  
  var timeStamp = ee.Number(image.get('system:time_start')).toByte(); // get the time stamp of each frame. This can be any string. Date, Years, Hours, etc.
  var timeStamp = ee.String('Date: ').cat(ee.String(timeStamp)); //convert time stamp to string 
  var image = image.visualize({ //convert each frame to RGB image explicitly since it is a 1 band image
      forceRgbOutput: true,
      min: 0.7672672672672672,
      max: 3.292857142857143,
      palette: ['midnightblue', 'blue', 'mediumblue', 'aqua', 'lime', 'limegreen', 'yellow','sandybrown', 'orange', 'crimson']
    }).set({'label':timeStamp}); // set a property called label for each image
  
  var annotated = text.annotateImage(image, {}, geometry, annotations); // create a new image with the label overlayed using gena's package

  return annotated 
}

var collection = collection.map(addText) //add time stamp to all images
  

print(ui.Thumbnail(collection,args));

How can I change the code or what property should I use to add the date in the format (YYYY/MM/DD)?

Best Answer

The issue is in:

var timeStamp = ee.Number(image.get('system:time_start')).toByte(); // get the time stamp of each frame. This can be any string. Date, Years, Hours, etc.

It needs to be changed to:

  var timeStamp = ee.Date(image.get('system:time_start')).format().slice(0,10); // get the time stamp of each frame. This can be any string. Date, Years, Hours, etc.

After above change, running code in this link produces expected result; as it can be observed in following picture (for 2019-07-05; but it automatically changes for the entire collection):

enter image description here