Read the current local calendar date with date.today().
How It Works
date.today() returns the host machine local calendar date, which may differ from the date in UTC or a user timezone. For location-specific scheduling, request an aware datetime in that region and then take its date.
Step-by-Step Example
from datetime import date, datetime, timezone
local_date = date.today()
utc_date = datetime.now(timezone.utc).date()
print(isinstance(local_date, date))
print(isinstance(utc_date, date))
Understanding the Example
- The expression 'date.today()' supplies the value stored as 'local_date'. This value is then used or checked by the later statements.
- The expression 'datetime.now(timezone.utc).date()' supplies the value stored as 'utc_date'. This value is then used or checked by the later statements.
Run the complete example as written, then inspect the displayed result. The values returned by the operation are more important than simply seeing the script finish.
Where This Fits in a Real Program
For event timestamps, distinguish the displayed local clock from the instant that it represents. Store or exchange an unambiguous aware value, then convert it when displaying it to a user. For repeated calendar appointments, use a geographic timezone so daylight-saving rules are not lost.
Practice: Try It Yourself
Use ZoneInfo for a specific location rather than trusting server timezone settings.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Assuming the server local date is always the date experienced by a remote user.
- Compare the host local date to the UTC date near a day boundary.
Key Takeaways
- Read the current local calendar date with date.today().
- date.today() returns the host machine local calendar date, which may differ from the date in UTC or a user timezone.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.