Python Dates

Python · Working with Data

Theory

The datetime module represents dates and times. datetime(2026, 7, 11) creates a specific moment, datetime.now() gives the current local time, and date holds a calendar date without a time. A timedelta is a duration; adding timedelta(days=30) to a date moves it 30 days forward, and subtracting two dates gives a timedelta.

To turn a date into text use strftime() with format codes: %d day, %m month number, %b short month name, %Y four-digit year, %A weekday name. So d.strftime('%d-%b-%Y') gives '11-Jul-2026'. The reverse, datetime.strptime('11-07-2026', '%d-%m-%Y'), parses text into a datetime. Think 'f' for format (to string) and 'p' for parse (from string).

Why we use it

Real data is full of dates - order dates, joining dates, timestamps. datetime lets you parse them, compare them, do arithmetic like 'due in 30 days' and print them in whatever format a report needs.

When to use it

Use datetime for individual dates in plain Python. For whole columns of dates in a DataFrame, pandas' pd.to_datetime() and the .dt accessor are faster and more convenient.

How it works

  1. Start with text '11-07-2026' or values
  2. Is the input text? Yes: Parse with datetime.strptime(text, fmt). No: Build with datetime(y, m, d).
  3. Do arithmetic with timedelta if needed
  4. Format output with strftime(fmt)
  5. Readable date string such as '11-Jul-2026'

Syntax

from datetime import datetime
datetime.now()

Example

d = datetime(2026, 7, 11)
d.strftime('%d-%b-%Y')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic