[GIS] get date from image using python API in Google Earth Engine

google-earth-enginepython

I am trying to extract the date as a string from an image in google earth engine, but can't seem to get it right. This is the code I have so far.

date = ee.Date(image.get('system:time_start'))

This returns an ee.date object in Python, but I can't find the right way to access the object. Date.getInfo() returns an error.

Does anyone know how to do this?

Best Answer

It is possible the access the date in the following way:

import ee

ee.Initialize()

collection = ee.ImageCollection('LANDSAT/LC08/C01/T1')

date = ee.Date(collection.first().get('system:time_start'))

date_dict = date.getInfo()

print "The number of milliseconds since 1970-01-01T00:00:00Z.: ", date_dict['value']

print "Formatted date", date.format('Y-M-d').getInfo()

Just calling ".getInfo()" on the ee.Date object will give you the number of milliseconds since 1970-01-01 in a Python dictionary.

It is possible to format the date with the ".format" method.