ee.Date.advance

Create a new Date by adding the specified units to the given Date.

UsageReturns
Date.advance(delta, unit, timeZone)Date
ArgumentTypeDetails
this: dateDate

deltaFloat

unitString

One of 'year', 'month' 'week', 'day', 'hour', 'minute', or 'second'.

timeZoneString, default: null

The time zone (e.g. 'America/Los_Angeles'); defaults to UTC.

Examples

JavaScript

var date = ee.Date('2020-7-1T13:00', 'UTC');

print(date.advance(1, 'second'));  // Date (2020-07-01 13:00:01)
// Plural is allowed.
print(date.advance(1, 'seconds'));  // Date (2020-07-01 13:00:01)
// Case insensitive.
print(date.advance(1, 'SECONDS'));  // Date (2020-07-01 13:00:01)
print(date.advance(1, 'minute'));   // Date (2020-07-01 13:01:00)
print(date.advance(1, 'hour'));     // Date (2020-07-01 14:00:00)
print(date.advance(1, 'day'));      // Date (2020-07-02 13:00:00)
print(date.advance(1, 'week'));     // Date (2020-07-08 13:00:00)
print(date.advance(1, 'month'));    // Date (2020-08-01 13:00:00)
print(date.advance(1, 'year'));     // Date (2021-07-01 13:00:00)

// Negative delta moves back in time.
print(date.advance(-1, 'second'));  // Date (2020-07-01 12:59:59)

// Leap years are handled.
print(ee.Date('2019-2-28').advance(1, 'day'));  // Date (2019-03-01 00:00:00)
print(ee.Date('2020-2-28').advance(1, 'day'));  // Date (2020-02-29 00:00:00)

// Time zones.
print(date.advance(5, 'month'));             // Date (2020-12-01 13:00:00)
print(date.advance(5, 'month', 'UTC'));      // Date (2020-12-01 13:00:00)
print(date.advance(5, 'month', 'PST8PDT'));  // Date (2020-12-01 14:00:00)

Python

"""Demonstrates the ee.Date.advance method."""

import datetime
import ee


def print_ee_date(ee_date):
  """Prints a string representation of an Earth Engine Date object."""
  print(datetime.datetime.utcfromtimestamp(ee_date.getInfo()['value'] / 1000.0))

# Authenticate to the Earth Engine servers.
ee.Authenticate()
# Initialize the client library.
ee.Initialize()

# Define a base date/time.
base_date = ee.Date('2020-01-01T00:00', 'UTC')
print('Base date/time:')
print_ee_date(base_date)

# Basic usage.
print('Advanced 1 week:')
print_ee_date(base_date.advance(1, 'week'))  # 2020-07-08 13:00:00
print('Advanced 1 year:')
print_ee_date(base_date.advance(1, 'year'))  # 2021-07-01 13:00:00

# Negative delta moves back in time.
print('Advanced -1 seconds:')
print_ee_date(base_date.advance(-1, 'second'))  # 2020-07-01 12:59:59

# Leap years are handled.
print('Advanced 1 day in a non-leap year:')
print_ee_date(ee.Date('2019-2-28').advance(1, 'day'))  # 2019-03-01 00:00:00
print('Advanced 1 day in a leap year:')
print_ee_date(ee.Date('2020-2-28').advance(1, 'day'))  # 2020-02-29 00:00:00