ee.Date.getFraction

  • Date.getFraction() calculates the elapsed fraction of a specified time unit (year, month, week, day, hour, minute, or second) for a given date, returning a value between 0 and 1.

  • The function takes the date, the desired time unit, and an optional time zone as input (defaulting to UTC if not specified).

  • The returned value represents the proportion of the specified time unit that has passed for the given date, for instance, 0.5 for 'day' would indicate it's midday.

  • This function is valuable for tasks like determining the progress through a specific time period, such as calculating the fraction of a year that has elapsed.

Returns this date's elapsed fraction of the specified unit (between 0 and 1).

UsageReturns
Date.getFraction(unit, timeZone)Float
ArgumentTypeDetails
this: dateDate
unitStringOne of 'year', 'month', 'week', 'day', 'hour', 'minute', or 'second'.
timeZoneString, default: nullThe time zone (e.g., 'America/Los_Angeles'); defaults to UTC.

Examples

Code Editor (JavaScript)

var date = ee.Date('2021-4-30T07:15:31.24');

print('Elapsed fraction of a year', date.getFraction('year'));
print('Elapsed fraction of a month', date.getFraction('month'));
print('Elapsed fraction of a week', date.getFraction('week'));
print('Elapsed fraction of a day', date.getFraction('day'));
print('Elapsed fraction of an hour', date.getFraction('hour'));
print('Elapsed fraction of a minute', date.getFraction('minute'));
print('Elapsed fraction of a second', date.getFraction('second'));

Python setup

See the Python Environment page for information on the Python API and using geemap for interactive development.

import ee
import geemap.core as geemap

Colab (Python)

date = ee.Date('2021-4-30T07:15:31.24')

display('Elapsed fraction of a year:', date.getFraction('year'))
display('Elapsed fraction of a month:', date.getFraction('month'))
display('Elapsed fraction of a week:', date.getFraction('week'))
display('Elapsed fraction of a day:', date.getFraction('day'))
display('Elapsed fraction of an hour:', date.getFraction('hour'))
display('Elapsed fraction of a minute:', date.getFraction('minute'))
display('Elapsed fraction of a second:', date.getFraction('second'))