datetime — Dates, Timezones & Durations — Concept Check
Questions on datetime — Dates, Timezones & Durations. datetime + timedelta do date arithmetic; strftime/strptime convert to and from strings; and timezone-aware UTC datetimes are the only correct choice for backends.
Questions in this quiz
8 questions on datetime — Dates, Timezones & Durations — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer needs to calculate the number of days until a project deadline. They have the current date and the deadline date as datetime objects. Which approach should they use?
- AUse strftime to format both dates and then subtract.
- BUse strptime to parse both dates and then add.
- CAdd the current date to the deadline date to get a timedelta.
- DSubtract the current date from the deadline date to get a timedelta.
2.An engineer is storing timestamps in a database and needs to ensure they are consistent across different time zones. What should they do?
- AUse naive datetime objects for simplicity.
- BStore timestamps in local time zones for easier readability.
- CStore timestamps in UTC with timezone-aware objects.
- DConvert timestamps to strings before storing.
3.A service needs to check if a token is still valid 12 hours after it was issued. Which code snippet correctly implements this?
- Atoken_expiry = now + timedelta(minutes=720); now > token_expiry
- Btoken_expiry = now - timedelta(hours=12); now > token_expiry
- Ctoken_expiry = now + timedelta(hours=12); now < token_expiry
- Dtoken_expiry = now + timedelta(days=0.5); now < token_expiry
4.A developer is parsing a date string '2026-07-10T18:30:00+05:30' from an API. Which method should they use?
- Adatetime.strftime
- Bdatetime.fromisoformat
- Cdatetime.isoformat
- Ddatetime.strptime
5.When formatting a datetime object into a string for display, which method should be used?
- Astrftime
- Bfromisoformat
- Cisoformat
- Dstrptime
6.A software engineer notices an error when comparing a naive datetime with an aware datetime. What is the likely cause?
- AThe aware datetime object is missing timezone information.
- BThe naive datetime object is in the wrong format.
- CNaive and aware datetime objects cannot be directly compared.
- DThe comparison is done using incorrect operators.
7.Which of the following is true about the use of utcnow() in production code?
- Autcnow() is deprecated because it returns a naive datetime.
- Butcnow() is preferred for its simplicity.
- Cutcnow() automatically attaches timezone information.
- Dutcnow() formats datetime into a string.
8.What is the total number of seconds in a timedelta representing one day?
- A86400.0
- B43200.0
- C3600.0
- D7200.0