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 keeping the calendar portion and discarding the time portion. The exact code depends on the language, but the real design question is whether you are only formatting the value for display or changing its data type in memory or in a database.
Python: Use .date() on a datetime
In Python, a datetime.datetime object has a built-in .date() method:
Output:
This returns a true date object, not just a string. That distinction matters if you plan to compare, sort, or store the result.
Java: Convert LocalDateTime to LocalDate
In modern Java, the java.time package makes this straightforward:
If you are working with ZonedDateTime or Instant, convert to the correct time zone before taking the date. Otherwise, the calendar day can shift.
JavaScript: Decide Whether You Need a String or a Calendar Value
JavaScript does not have a separate built-in date-only type in the same way Python and Java do. Usually, developers either keep a Date object and ignore the time, or format a date-only string.
That prints:
Be careful here. toISOString() uses UTC. If your business logic depends on local time, build the date from local components instead:
SQL: Cast or Extract the Date Part
In SQL databases, the idea is similar, but syntax varies slightly by engine. A common pattern is:
In some systems, functions such as DATE(created_at) are also common:
This is useful for reporting and grouping, though it can affect index usage in some queries.
Conversion vs Formatting
This distinction is easy to miss. Formatting means turning the datetime into text:
That gives a readable string, but it is no longer a date object. If you need calendar arithmetic, a real date type is usually the better result.
The same idea applies in JavaScript. A date string is fine for display or API payloads, but it behaves very differently from a Date object.
Time Zone Order Matters
A timestamp represents a moment in time, while a date represents a calendar day in a specific zone or context. Convert the moment into the correct time zone before stripping the time portion.
For example, 2025-03-11 00:30 UTC may still be 2025-03-10 in another time zone. If you truncate too early, you can silently assign the wrong day.
Common Pitfalls
The most common mistake is formatting a datetime as text and assuming it is now a true date value. It may look correct on screen but behave differently in comparisons and calculations.
Another pitfall is ignoring time zones. A date extracted in UTC is not always the same as the date a user expects locally.
In JavaScript, developers often expect a real date-only type and end up with a string instead. That is fine if it is intentional, but it should be explicit.
In SQL, wrapping a datetime column in a function can make a query slower if it prevents the database from using an index efficiently. For large datasets, consider range predicates instead of converting every row during filtering.
Summary
- Converting a datetime to a date means removing the time portion while preserving the calendar meaning.
- In Python, use
.date(), and in Java use.toLocalDate(). - In JavaScript, you usually produce a date-only string or normalize a
Dateobject intentionally. - In SQL,
CAST(... AS DATE)orDATE(...)is the usual pattern. - Always handle time zone conversion before truncating the time, or you may get the wrong day.

