Datetime Conversion
Programming
Coding Tips
Date Format
Software Development

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:

python
1from datetime import datetime
2
3dt = datetime(2025, 3, 11, 14, 30, 0)
4only_date = dt.date()
5
6print(only_date)
7print(type(only_date))

Output:

text
2025-03-11
<class 'datetime.date'>

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:

java
1import java.time.LocalDate;
2import java.time.LocalDateTime;
3
4public class Main {
5    public static void main(String[] args) {
6        LocalDateTime dt = LocalDateTime.of(2025, 3, 11, 14, 30);
7        LocalDate date = dt.toLocalDate();
8
9        System.out.println(date);
10    }
11}

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.

javascript
1const dt = new Date("2025-03-11T14:30:00Z");
2
3const dateOnly = dt.toISOString().slice(0, 10);
4console.log(dateOnly);

That prints:

text
2025-03-11

Be careful here. toISOString() uses UTC. If your business logic depends on local time, build the date from local components instead:

javascript
1const dt = new Date();
2const yyyy = dt.getFullYear();
3const mm = String(dt.getMonth() + 1).padStart(2, "0");
4const dd = String(dt.getDate()).padStart(2, "0");
5
6const localDate = `${yyyy}-${mm}-${dd}`;
7console.log(localDate);

SQL: Cast or Extract the Date Part

In SQL databases, the idea is similar, but syntax varies slightly by engine. A common pattern is:

sql
SELECT CAST(created_at AS DATE) AS created_date
FROM orders;

In some systems, functions such as DATE(created_at) are also common:

sql
SELECT DATE(created_at) AS created_date
FROM orders;

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:

python
print(dt.strftime("%Y-%m-%d"))

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 Date object intentionally.
  • In SQL, CAST(... AS DATE) or DATE(...) is the usual pattern.
  • Always handle time zone conversion before truncating the time, or you may get the wrong day.

Course illustration
Course illustration

All Rights Reserved.