Back to Freecodecamp

Challenge 239: What Day Is It?

curriculum/challenges/english/blocks/daily-coding-challenges-python/69b559d2903b9e4afe9075f9.md

latest1.6 KB
Original Source

--description--

Given a Unix timestamp in milliseconds, return the day of the week.

Valid return days are:

  • "Sunday"
  • "Monday"
  • "Tuesday"
  • "Wednesday"
  • "Thursday"
  • "Friday"
  • "Saturday"

Be sure to ignore time zones.

--hints--

get_day_of_week(1775492249000) should return "Monday".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(1775492249000), "Monday")`)
}})

get_day_of_week(1766246400000) should return "Saturday".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(1766246400000), "Saturday")`)
}})

get_day_of_week(33791256000000) should return "Tuesday".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(33791256000000), "Tuesday")`)
}})

get_day_of_week(1773576000000) should return "Sunday".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(1773576000000), "Sunday")`)
}})

get_day_of_week(0) should return "Thursday".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(0), "Thursday")`)
}})

--seed--

--seed-contents--

py
def get_day_of_week(timestamp):

    return timestamp

--solutions--

py
from datetime import datetime, timezone

def get_day_of_week(timestamp):
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    dt = datetime.fromtimestamp(timestamp / 1000, tz=timezone.utc)
    return days[dt.weekday()]