How do I convert a datetime to date?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting a datetime to a date means removing the time portion and keeping only the calendar day. The exact code depends on the environment, but the underlying idea is always the same: extract the date component without accidentally changing the timezone or formatting meaning.
Python: use .date() on a datetime
In Python's standard library, the direct answer is .date():
This returns a real date object, not just a string.
Pandas: use the datetime accessor
If you are working with a DataFrame column, use the .dt accessor:
This is the normal way to strip time from every value in a datetime-like pandas column.
SQL: cast or convert to DATE
In SQL, the common approach is casting:
Different databases offer equivalent functions, but the core idea is the same: change the datetime expression into a date type so only the calendar portion remains.
JavaScript: be careful about strings versus actual date-only values
JavaScript does not have a built-in date type separate from Date, so many examples produce a date string instead of a date-only object.
This is fine when you need the text form YYYY-MM-DD, but it is not the same thing as Python returning a distinct date object.
Why timezone awareness matters
Datetime-to-date conversion sounds simple, but timezones can change the calendar day. A timestamp near midnight in one timezone may fall on the next or previous day in another.
For example, converting after a timezone shift:
That date is correct for UTC, but if you first convert the timestamp to another timezone, the date could change. So the safe rule is: convert to the intended timezone first, then extract the date. That order matters in reporting, billing, scheduling, and any workflow where the business day is more important than the raw timestamp.
If you are designing an API or a database schema, it is worth deciding whether you want to store a full timestamp or a date-only value. Converting deliberately at the boundaries of the system often keeps later logic much simpler.
Common Pitfalls
The biggest mistake is confusing a date object with a date string. They look similar when printed, but they behave differently in code.
Another common issue is dropping the time component before applying the correct timezone. That can shift records into the wrong business day.
Be careful with pandas too. .dt.date gives Python date objects, which is fine in many cases, but sometimes you may want to keep a pandas datetime-like dtype instead for further vectorized operations.
Finally, do not assume the same syntax works everywhere. Python, SQL, pandas, and JavaScript solve the same problem with different APIs and return types. Being explicit about the target type saves a lot of downstream confusion.
Summary
- In Python, use
.date()on adatetimeobject. - In pandas, use
.dt.dateon a datetime-like column. - In SQL, cast or convert the datetime expression to
DATE. - In JavaScript, you usually produce a date string rather than a separate date-only object.
- Apply timezone logic before extracting the date if the calendar day matters.

