sciris.sc_datetime.date

date(obj, *args, start_date=None, readformat=None, outformat=None, as_date=True, **kwargs)[source]

Convert any reasonable object – a string, integer, or datetime object, or list/array of any of those – to a date object. To convert an integer to a date, you must supply a start date.

Caution: while this function and sc.readdate() are similar, and indeed this function calls sc.readdate() if the input is a string, in this function an integer is treated as a number of days from start_date, while for sc.readdate() it is treated as a timestamp in seconds.

Note: in this and other date functions, arguments work either with or without underscores (e.g. start_date or startdate)

Parameters
  • obj (str/int/date/datetime/list/array) – the object to convert

  • args (str/int/date/datetime) – additional objects to convert

  • start_date (str/date/datetime) – the starting date, if an integer is supplied

  • readformat (str/list) – the format to read the date in; passed to sc.readdate()

  • outformat (str) – the format to output the date in, if returning a string

  • as_date (bool) – whether to return as a datetime date instead of a string

Returns

either a single date object, or a list of them (matching input data type where possible)

Return type

dates (date or list)

Examples:

sc.date('2020-04-05') # Returns datetime.date(2020, 4, 5)
sc.date([35,36,37], start_date='2020-01-01', as_date=False) # Returns ['2020-02-05', '2020-02-06', '2020-02-07']
sc.date(1923288822, readformat='posix') # Interpret as a POSIX timestamp
New in version 1.0.0.
New in version 1.2.2: “readformat” argument; renamed “dateformat” to “outformat”
New in version 2.0.0: support for np.datetime64 objects