def date_to_decimal_year(date, format=None):
    'Convert a date to decimal year.\n\n    Args:\n        date (str): Date as a string.\n        format (str, optional): Format of the date if a conversion is needed.\n\n    Returns:\n        float: Decimal year corresponding to the date.\n    '
    if format:
        date = datetime.datetime.strptime(date, format)
    start = datetime.date(date.year, 1, 1).toordinal()
    year_length = (datetime.date((date.year + 1), 1, 1).toordinal() - start)
    subday_time = 0
    if hasattr(date, 'hour'):
        subday_time += ((date.hour / year_length) / 24)
    if hasattr(date, 'minute'):
        subday_time += (((date.minute / year_length) / 24) / 60)
    if hasattr(date, 'second'):
        subday_time += ((((date.second / year_length) / 24) / 60) / 60)
    return ((date.year + (float((date.toordinal() - start)) / year_length)) + subday_time)
Convert a date to decimal year.

Args:
    date (str): Date as a string.
    format (str, optional): Format of the date if a conversion is needed.

Returns:
    float: Decimal year corresponding to the date.
def date_to_decimal_year(date, format=None):
    'Convert a date to decimal year.\n\n    Args:\n        date (str): Date as a string.\n        format (str, optional): Format of the date if a conversion is needed.\n\n    Returns:\n        float: Decimal year corresponding to the date.\n    '
    if format:
        date = datetime.datetime.strptime(date, format)
    start = datetime.date(date.year, 1, 1).toordinal()
    year_length = (datetime.date((date.year + 1), 1, 1).toordinal() - start)
    subday_time = 0
    if hasattr(date, 'hour'):
        subday_time += ((date.hour / year_length) / 24)
    if hasattr(date, 'minute'):
        subday_time += (((date.minute / year_length) / 24) / 60)
    if hasattr(date, 'second'):
        subday_time += ((((date.second / year_length) / 24) / 60) / 60)
    return ((date.year + (float((date.toordinal() - start)) / year_length)) + subday_time)