{"id":"dates-datetime","slug":"datetime-objects","title":"Dates and times","text":"Dates are trickier than they look. Months have 28 to 31 days, every fourth year or so has a February 29, and a week from December 28 is in another year. The standard library's **`datetime`** module knows all of that. Its most used part is a type that is also called `datetime`, so programs usually start with `from datetime import datetime`: from the module `datetime`, import the type `datetime`.\n\n### Making a datetime\n\nA `datetime` value holds one moment: a date and a time of day. You make one by giving the year, the month and the day, and if you like, the hour and the minute:\n\n```python\nfrom datetime import datetime\n\nmoon_landing = datetime(1969, 7, 20, 20, 17)\nprint(moon_landing)\nnew_year = datetime(2027, 1, 1)\nprint(new_year)\n```\n\n```output\n1969-07-20 20:17:00\n2027-01-01 00:00:00\n```\n\nWithout a time, the moment is midnight, `00:00:00`. Printed, a datetime shows the year first, then the month and the day, then the hours, minutes and seconds: the international standard order, which sorts correctly as text too.\n\n### The parts of a datetime\n\nA datetime stores its parts as **attributes**, values you reach with a dot and their name: `year`, `month`, `day`, `hour`, `minute` and `second`. They're stored values, not functions, so there are no parentheses after them. The method `weekday()` does have parentheses: it calculates the day of the week, 0 for Monday up to 6 for Sunday.\n\n```python\nfrom datetime import datetime\n\nmeeting = datetime(2026, 11, 3, 14, 30)\nprint(\"Year:\", meeting.year)\nprint(\"Month:\", meeting.month)\nprint(\"Day:\", meeting.day)\nprint(f\"Time: {meeting.hour}:{meeting.minute}\")\nprint(\"Weekday:\", meeting.weekday())\n```\n\n```output\nYear: 2026\nMonth: 11\nDay: 3\nTime: 14:30\nWeekday: 1\n```\n\nNovember 3, 2026 is a Tuesday, day 1 when Monday is day 0.\n\n### Only real dates\n\nA datetime can only hold a date that exists. Ask for one that doesn't, and Python stops with a `ValueError` that says what's wrong:\n\n```python\nfrom datetime import datetime\n\ndeadline = datetime(2026, 2, 30)\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 3, in <module>\n    deadline = datetime(2026, 2, 30)\nValueError: day 30 must be in range 1..28 for month 2 in year 2026\n```\n\nThat makes it a handy checker: with `try` and `except ValueError`, a program can find out whether a date a user typed exists.\n\n### The current time\n\n`datetime.now()` gives the moment it's called, the date and time on the computer's clock. Its output is different every second, so the examples and checks on this site use fixed dates instead. On your own computer, try:\n\n```python\nfrom datetime import datetime\n\nprint(datetime.now())\n```\n","task":"Ada Lovelace was born on 10 December 1815. Fill in the blanks so the program makes that date and prints it, and then prints only its year.\n","kind":"fill","seed":"from datetime import datetime\n\nbirthday = datetime([[1815]], [[12]], [[10]])\nprint(birthday)\nprint(birthday.[[year]])\n","solution":"from datetime import datetime\n\nbirthday = datetime(1815, 12, 10)\nprint(birthday)\nprint(birthday.year)\n","hints":["The parts go in the order year, month, day: the biggest unit first.","December is month 12. The last blank is the attribute that holds the year: `year`, without parentheses."],"tests":[{"name":"Output","stdin":"","expect":"1815-12-10 00:00:00\n1815","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"from datetime import datetime\n\nmoment = datetime(2026, 12, 31, 23, 59)\nprint(moment)\nprint(moment.month, moment.day)\nprint(moment.minute + 1)\n","solution":"from datetime import datetime\n\nmoment = datetime(2026, 12, 31, 23, 59)\nprint(moment)\nprint(moment.month, moment.day)\nprint(moment.minute + 1)\n","hints":["A printed datetime shows the date as year-month-day, then the time with seconds. The seconds weren't given, so they're 0.","`moment.month` and `moment.day` are plain integers, and `print` puts a space between them.","`moment.minute` is the integer 59. Adding 1 is ordinary arithmetic; it doesn't move the date to the next hour."],"tests":[{"name":"Output","stdin":"","expect":"2026-12-31 23:59:00\n12 31\n60","hidden":false}],"require":[],"forbid":[],"lines":["2026-12-31 23:59:00","12 31","60"]},{"task":"Write a program that asks for a year, a month and a day, and tells whether that date exists. For a real date it prints the date and its weekday name, and otherwise `There is no such date.` Make the date with `datetime(...)` inside `try`, and catch the `ValueError` it raises for a date that doesn't exist. For example:\n\n```\nYear: 2028\nMonth: 2\nDay: 29\n29.2.2028 is a Tuesday\n```\n\nThe weekday names are in the starter code: `weekday()` gives the index into the list.\n","kind":"code","seed":"from datetime import datetime\n\nweekdays = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]\n","solution":"from datetime import datetime\n\nweekdays = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]\n\nyear = int(input(\"Year: \"))\nmonth = int(input(\"Month: \"))\nday = int(input(\"Day: \"))\ntry:\n    date = datetime(year, month, day)\n    print(f\"{date.day}.{date.month}.{date.year} is a {weekdays[date.weekday()]}\")\nexcept ValueError:\n    print(\"There is no such date.\")\n","hints":["Read the three numbers with `int(input(...))`, using the prompts of the example.","Put `date = datetime(year, month, day)` and the line that prints it inside `try`. If the date doesn't exist, Python jumps to `except ValueError:`.","The weekday's name is `weekdays[date.weekday()]`."],"tests":[{"name":"Test 1","stdin":"2028\n2\n29\n","expect":"Year: 2028\nMonth: 2\nDay: 29\n29.2.2028 is a Tuesday","hidden":false},{"name":"Test 2","stdin":"2026\n2\n29\n","expect":"Year: 2026\nMonth: 2\nDay: 29\nThere is no such date.","hidden":true},{"name":"Test 3","stdin":"2026\n4\n31\n","expect":"Year: 2026\nMonth: 4\nDay: 31\nThere is no such date.","hidden":true},{"name":"Test 4","stdin":"2026\n12\n31\n","expect":"Year: 2026\nMonth: 12\nDay: 31\n31.12.2026 is a Thursday","hidden":true},{"name":"Test 5","stdin":"2026\n13\n1\n","expect":"Year: 2026\nMonth: 13\nDay: 1\nThere is no such date.","hidden":true}],"require":[],"forbid":[]}]}