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

A Developer's Guide to the OpenAI Deep Research API

A Developer's Guide to the OpenAI Deep Research API

In the age of information overload, the ability to conduct fast, accurate, and comprehensive research is a superpower. Developers, analysts, and strategists spend countless hours sifting through documents, verifying sources, and synthesizing findings. What if you could automate this entire workflow? OpenAI's Deep Research API is a significant step in that direction, offering a powerful tool to transform high-level questions into structured, citation-rich reports. The Deep Research API isn't jus

27 June 2025

How to Get Free Gemini 2.5 Pro Access + 1000 Daily Requests (with Google Gemini CLI)

How to Get Free Gemini 2.5 Pro Access + 1000 Daily Requests (with Google Gemini CLI)

Google's free Gemini CLI, the open-source AI agent, rivals its competitors with free access to 1000 requests/day and Gemini 2.5 pro. Explore this complete Gemini CLI setup guide with MCP server integration.

27 June 2025

How to Use MCP Servers in LM Studio

How to Use MCP Servers in LM Studio

The world of local Large Language Models (LLMs) represents a frontier of privacy, control, and customization. For years, developers and enthusiasts have run powerful models on their own hardware, free from the constraints and costs of cloud-based services.However, this freedom often came with a significant limitation: isolation. Local models could reason, but they could not act. With the release of version 0.3.17, LM Studio shatters this barrier by introducing support for the Model Context Proto

26 June 2025

Practice API Design-first in Apidog

Discover an easier way to build and use APIs