datetime conversion
date extraction
datetime to date
programming tips
datetime manipulation

How do I convert a datetime to date?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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():

python
1from datetime import datetime
2
3stamp = datetime(2025, 9, 23, 14, 30, 45)
4only_date = stamp.date()
5
6print(stamp)      # 2025-09-23 14:30:45
7print(only_date)  # 2025-09-23
8print(type(only_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:

python
1import pandas as pd
2
3frame = pd.DataFrame({
4    'created_at': pd.to_datetime([
5        '2025-09-23 14:30:45',
6        '2025-09-24 08:10:00',
7    ])
8})
9
10frame['created_date'] = frame['created_at'].dt.date
11print(frame)

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:

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

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.

javascript
1const stamp = new Date('2025-09-23T14:30:45Z');
2const dateOnly = stamp.toISOString().slice(0, 10);
3
4console.log(dateOnly); // 2025-09-23

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:

python
1from datetime import datetime, timezone
2
3stamp = datetime(2025, 9, 23, 23, 30, tzinfo=timezone.utc)
4print(stamp.date())

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 a datetime object.
  • In pandas, use .dt.date on 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.