Polly is 8 years old. She is eagerly awaiting Christmas as she has a bone to pick with Santa Claus. Last year she asked for a horse, and he brought her a dolls house. Understandably she is livid.

The days seem to drag and drag so Polly asks her friend to help her keep count of how long it is until Christmas, in days. She will start counting from the first of December.

Your function should take 1 argument (a Date object) which will be the day of the year it is currently. The function should then work out how many days it is until Christmas.

Watch out for leap years!

Difficulty: introductory

Starter code:

def days_until_christmas(day):
	

Solutions:
from datetime import date


def days_until_christmas(day):
    christmas = date(day.year, 12, 25)
    if day > christmas:
        christmas = date(day.year + 1, 12, 25)
    return (christmas - day).days


from datetime import date, timedelta

def days_until_christmas(day):
    next = day + timedelta(days=6) # Increments year if between christmas and new years
    return (date(next.year,12,25) - day).days

def days_until_christmas(dt):

  is_after = int(dt.month==12 and dt.day>25)
  christmas = dt.replace(month=12, day=25, year = dt.year + is_after)

  return (christmas - dt).days

def days_until_christmas(day):
    return (date(day.year + (1 if day.month > 11 and day.day > 25 else 0), 12, 25) - day).days

def days_until_christmas(day):
    christmas = date(day.year + ((day.month, day.day) > (12, 25)), 12, 25)
    return (christmas - day).days

from datetime import date
def days_until_christmas(day):
    return (date(day.year + (day.month == 12 and day.day > 25), 12, 25) - day).days

import datetime
def days_until_christmas(year):
   
    k=year.year
    
    dc=datetime.date(k,12,25)
    r=dc-year
    
    ret=r.days
    
    if ret>=0:
        return ret
    else:
        dc=datetime.date(k+1,12,25)
        r=dc-year
       
        ret=r.days
        
        return ret

def days_until_christmas(day):
    CHRISTMAS = date(day.year,12,25)
    if day > CHRISTMAS:
        CHRISTMAS = CHRISTMAS.replace(year=day.year + 1)
    return (CHRISTMAS - day).days

import datetime

def days_until_christmas(today):
    noel = datetime.date(today.year, 12, 25)
    result = (noel-today).days
    return result if result >= 0 else (datetime.date(noel.year+1,noel.month,noel.day)-today).days

from datetime import date

def days_until_christmas(day):
    this_christmas = date(day.year, 12, 25)
    next_christmas = date(day.year + 1, 12, 25)
    return (this_christmas - day if day <= this_christmas else next_christmas - day).days