datedelta#
- datedelta(datestr=None, days=0, months=0, years=0, weeks=0, dt1=None, dt2=None, as_date=None, **kwargs)[source]#
Perform calculations on a date string (or date object), returning a string (or a date). Wrapper to
dateutil.relativedelta.relativedelta()
.If
datestr
isNone
, then return the delta object rather than the new date.- Parameters:
datestr (None/str/date/list) – the starting date (typically a string); if None, return the relative delta
days (int) – the number of days (positive or negative) to increment
months (int) – as above
years (int/float) – as above; if a float, converted to days (NB: fractional months and weeks are not supported)
weeks (int) – as above
dt1 (dates) – if both provided, compute the difference between them
dt2 (dates) – if both provided, compute the difference between them
as_date (bool) – if True, return a date object; otherwise, return as input type
Examples:
sc.datedelta('2021-07-07', 3) # Add 3 days sc.datedelta('2021-07-07', days=-4) # Subtract 4 days sc.datedelta('2021-07-07', weeks=4, months=-1, as_date=True) # Add 4 weeks but subtract a month, and return a dateobj sc.datedelta(days=3) # Alias to du.relativedelta.relativedelta(days=3) sc.datedelta(['2021-07-07', '2022-07-07'], months=1) # Increment multiple dates sc.datedelta('2020-06-01', years=0.25) # Use a fractional number of years (to the nearest day)
New in version 3.0.0: operate on list of datesNew in version 3.1.0: handle all date input formatsNew in version 3.2.0: handle fractional years