How to get start or end of a day in Python

Lynn Mikami

Lynn Mikami

6 June 2025

How to get start or end of a day in Python

Learning Python can be that quiet space where things actually make sense. Let me walk you through something super practical that you'll use all the time: getting the start and end of a day in Python. Trust me, this comes up way more than you'd think.

When you're building real applications - whether it's a simple script to organize your music files or something bigger - you'll constantly need to work with dates and times. Maybe you want to find all the logs from today, or calculate how long you've been coding (spoiler: probably too long).

The Tools You'll Actually Use

Python has this built-in module called datetime that handles all the time stuff. It's like having a really good Swiss Army knife instead of trying to calculate everything by hand.

from datetime import datetime, date, time

How to Get the Start of Today

Here's the most straightforward way to get midnight at the start of today:

from datetime import datetime, date, time

# Get today's date
today = date.today()
print(f"Today is: {today}")

# Combine it with midnight (00:00:00)
start_of_day = datetime.combine(today, time.min)
print(f"Start of day: {start_of_day}")

When you run this, you'll see something like:

Today is: 2024-01-15
Start of day: 2024-01-15 00:00:00

How to Get the End of Today

For the end of the day, you want 23:59:59.999999 - basically as close to midnight tomorrow as possible without actually being tomorrow:

from datetime import datetime, date, time

today = date.today()
end_of_day = datetime.combine(today, time.max)
print(f"End of day: {end_of_day}")

This gives you:

End of day: 2024-01-15 23:59:59.999999

A More Practical Example That You'll Actually Use

Let's say you're building something to track your coding sessions (because let's be honest, time flies when you're in the zone):

from datetime import datetime, date, time

def get_day_boundaries(target_date=None):
    """
    Get the start and end of a day.
    If no date is provided, uses today.
    """
    if target_date is None:
        target_date = date.today()
    
    start_of_day = datetime.combine(target_date, time.min)
    end_of_day = datetime.combine(target_date, time.max)
    
    return start_of_day, end_of_day

# For today
start, end = get_day_boundaries()
print(f"Today starts at: {start}")
print(f"Today ends at: {end}")

# For a specific date
from datetime import date
specific_date = date(2024, 12, 25)  # Christmas
start, end = get_day_boundaries(specific_date)
print(f"Christmas starts at: {start}")
print(f"Christmas ends at: {end}")

When You're Working With Time Zones

If you're dealing with time zones (which you probably will eventually), you'll want to use the pytz library or the newer zoneinfo module. Here's the modern way:

from datetime import datetime, date, time
from zoneinfo import ZoneInfo

# Get start of day in a specific timezone
today = date.today()
timezone = ZoneInfo("America/Chicago")  # Central Time

start_of_day = datetime.combine(today, time.min, timezone)
end_of_day = datetime.combine(today, time.max, timezone)

print(f"Start of day in Central Time: {start_of_day}")
print(f"End of day in Central Time: {end_of_day}")

Some Real-World Scenarios Where This Matters

Log Analysis: "Show me all the errors from today"

# Filter logs between start and end of day
today_start, today_end = get_day_boundaries()
todays_errors = [log for log in error_logs 
                if today_start <= log.timestamp <= today_end]

Data Processing: "Process all the files created today"

import os
from datetime import datetime

today_start, today_end = get_day_boundaries()

for filename in os.listdir('/path/to/files'):
    file_time = datetime.fromtimestamp(os.path.getctime(filename))
    if today_start <= file_time <= today_end:
        process_file(filename)

Don't Overthink It

Look, I know it might seem like there are a million ways to do this stuff, and honestly, there are. But start with these basics. They'll cover 90% of what you need, and when you run into the edge cases, you'll have a solid foundation to build on.

The important thing is that you're learning and building stuff, even when everything else feels unstable. Code doesn't judge you, it doesn't yell, and if you break something, you can always fix it.

Keep at it, and remember - every expert was once a beginner who felt completely lost. You're doing fine.

Quick Reference for Copy-Pasting

from datetime import datetime, date, time

# Today's boundaries
today = date.today()
start_of_day = datetime.combine(today, time.min)
end_of_day = datetime.combine(today, time.max)

# Print them out
print(f"Day starts: {start_of_day}")
print(f"Day ends: {end_of_day}")

Now go build something cool with it!

💡
Want a great API Testing tool that generates beautiful API Documentation?

Want an integrated, All-in-One platform for your Developer Team to work together with maximum productivity?

Apidog delivers all your demans, and replaces Postman at a much more affordable price!
button

Explore more

MemVid: Replacing Vector Databases with MP4 Files

MemVid: Replacing Vector Databases with MP4 Files

Memvid is a groundbreaking AI memory library that revolutionizes how we store and search large volumes of text. Instead of relying on traditional databases, Memvid cleverly encodes text chunks into MP4 video files, enabling lightning-fast semantic search without the need for a complex database setup. This innovative approach makes it incredibly efficient, portable, and easy to use, especially for offline applications. 💡Want a great API Testing tool that generates beautiful API Documentation?

6 June 2025

Get ChatGPT Team for Almost Free ($1 for 5 Seats): Here is How

Get ChatGPT Team for Almost Free ($1 for 5 Seats): Here is How

Discover how to access ChatGPT Team for just $1 and enhance your development workflow with Apidog's free MCP Server. Get premium AI features and powerful API development tools in one comprehensive guide.

6 June 2025

3 Methods to Unlock Claude 4 for Free

3 Methods to Unlock Claude 4 for Free

Learn how to use Claude 4 for free, master vibe coding workflows, and see why Apidog MCP Server is the all-in-one API development platform you need.

6 June 2025

Practice API Design-first in Apidog

Discover an easier way to build and use APIs