Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-23 UTC."],[[["\u003cp\u003e\u003ccode\u003eDate.getRelative()\u003c/code\u003e returns the 0-based position of a specified time unit (e.g., day, month) within a larger time unit (e.g., year, month).\u003c/p\u003e\n"],["\u003cp\u003eThe function takes 'unit', 'inUnit', and an optional 'timeZone' as arguments to define the relative position and time zone context.\u003c/p\u003e\n"],["\u003cp\u003eValid units include 'year', 'month', 'week', 'day', 'hour', 'minute', and 'second'.\u003c/p\u003e\n"],["\u003cp\u003eIf the 'unit' is larger than the 'inUnit', the function returns 0.\u003c/p\u003e\n"]]],[],null,["Returns the specified (0-based) unit of this date relative to a larger unit, e.g., getRelative('day', 'year') returns a value between 0 and 365.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|------------------------------------------------|---------|\n| Date.getRelative`(unit, inUnit, `*timeZone*`)` | Long |\n\n| Argument | Type | Details |\n|--------------|-----------------------|---------------------------------------------------------------|\n| this: `date` | Date | |\n| `unit` | String | One of 'month', 'week', 'day', 'hour', 'minute', or 'second'. |\n| `inUnit` | String | One of 'year', 'month', 'week', 'day', 'hour', or 'minute'. |\n| `timeZone` | String, default: null | The time zone (e.g., 'America/Los_Angeles'); defaults to UTC. |\n\nExamples\n\nCode Editor (JavaScript) \n\n```javascript\nvar date = ee.Date('2021-4-30T07:15:31.24');\n\nprint('0-based month of year', date.getRelative('month', 'year'));\nprint('0-based week of year', date.getRelative('week', 'year'));\nprint('0-based day of year', date.getRelative('day', 'year'));\nprint('0-based day of month', date.getRelative('day', 'month'));\nprint('0-based minute of day', date.getRelative('minute', 'day'));\nprint('0-based second of minute', date.getRelative('second', 'minute'));\n\n// 0 is returned when unit argument is larger than inUnit argument.\nprint('0-based year of month (bad form)', date.getRelative('year', 'month'));\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\nColab (Python) \n\n```python\ndate = ee.Date('2021-4-30T07:15:31.24')\n\ndisplay('0-based month of year:', date.getRelative('month', 'year'))\ndisplay('0-based week of year:', date.getRelative('week', 'year'))\ndisplay('0-based day of year:', date.getRelative('day', 'year'))\ndisplay('0-based day of month:', date.getRelative('day', 'month'))\ndisplay('0-based minute of day:', date.getRelative('minute', 'day'))\ndisplay('0-based second of minute:', date.getRelative('second', 'minute'))\n\n# 0 is returned when unit argument is larger than inUnit argument.\ndisplay('0-based year of month (bad form):', date.getRelative('year', 'month'))\n```"]]