---
title: "Explain the Python Datetime with Example."  
description: "Explain the Python Datetime with Example."  
author: "Gulab"  
published: 2025-10-07  
updated: 2025-10-23  
canonical: https://www.mindstick.com/forum/161951/explain-the-python-datetime-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 4 minutes  

---

# Explain the Python Datetime with Example.

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) [Datetime](https://www.mindstick.com/forum/12949/how-to-validate-if-a-datetime-field-is-not-null-empty) with Example.**

## Replies

### Reply by Mayank kumar Verma

**Definition:**\
`datetime` module in Python is used to work with dates and times — like getting current date, formatting, or calculating time differences.\
from datetime import datetime\
now = datetime.now()\
print("Current Date and Time:", now)\
Current Date and Time: 2025-10-23 14:35:22

### Reply by ICSM Computer

## 1. What is `datetime` in Python?

> The `datetime` module in Python allows you to **work with dates and times** — creating, formatting, manipulating, and comparing them.

To use it:

```python
import datetime
```

## 2. Creating Date and Time Objects

You can create specific date/time objects using `datetime.date`, `datetime.time`, or `datetime.datetime`.

### Date object

```python
from datetime import date

today = date.today()
print("Today's date:", today)

custom_date = date(2025, 10, 9)
print("Custom date:", custom_date)
```

## Output:

```plaintext
Today's date: 2025-10-09
Custom date: 2025-10-09
```

### Datetime object

```python
from datetime import datetime

now = datetime.now()
print("Current datetime:", now)

custom_datetime = datetime(2025, 10, 9, 10, 30, 45)
print("Custom datetime:", custom_datetime)
```

## Output:

```plaintext
Current datetime: 2025-10-09 10:45:22.540123
Custom datetime: 2025-10-09 10:30:45
```

## 3. Getting Components (Year, Month, etc.)

You can extract parts of the date/time:

```python
from datetime import datetime

now = datetime.now()

print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)
```

## Output:

```plaintext
Year: 2025
Month: 10
Day: 9
Hour: 10
Minute: 45
Second: 22
```

## 4. Date Arithmetic (Add / Subtract Time)

You can add or subtract time using `timedelta`.

```python
from datetime import datetime, timedelta

today = datetime.now()
print("Today:", today)

tomorrow = today + timedelta(days=1)
yesterday = today - timedelta(days=1)
after_2_hours = today + timedelta(hours=2)

print("Tomorrow:", tomorrow)
print("Yesterday:", yesterday)
print("After 2 hours:", after_2_hours)
```

## Output:

```plaintext
Today: 2025-10-09 10:45:22
Tomorrow: 2025-10-10 10:45:22
Yesterday: 2025-10-08 10:45:22
After 2 hours: 2025-10-09 12:45:22
```

## 5. Formatting Dates and Times

Use `.strftime()` to **format datetime into strings**.

```python
from datetime import datetime

now = datetime.now()

formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted datetime:", formatted)

# Common patterns
print(now.strftime("%d/%m/%Y"))  # DD/MM/YYYY
print(now.strftime("%B %d, %Y"))  # Month name DD, YYYY
print(now.strftime("%I:%M %p"))  # 12-hour clock with AM/PM
```

## Output:

```plaintext
Formatted datetime: 2025-10-09 10:45:22
09/10/2025
October 09, 2025
10:45 AM
```

## 6. Parsing String to Datetime

Use `datetime.strptime()` to **convert string → datetime**.

```python
from datetime import datetime

date_str = "09/10/2025 14:30"
parsed_date = datetime.strptime(date_str, "%d/%m/%Y %H:%M")

print("Parsed datetime:", parsed_date)
```

## Output:

```plaintext
Parsed datetime: 2025-10-09 14:30:00
```

## 7. Getting UTC Time and Timezones

Python’s `datetime` can handle UTC and time zones using `timezone`.

```python
from datetime import datetime, timezone, timedelta

utc_now = datetime.now(timezone.utc)
print("UTC time:", utc_now)

# Convert UTC to IST (UTC+5:30)
ist = utc_now + timedelta(hours=5, minutes=30)
print("IST time:", ist)
```

## Output:

```plaintext
UTC time: 2025-10-09 05:15:22+00:00
IST time: 2025-10-09 10:45:22+05:30
```

## 8. Comparing Dates

You can directly compare two `datetime` or `date` objects:

```python
from datetime import date

d1 = date(2025, 10, 9)
d2 = date(2025, 12, 25)

if d1 < d2:
    print("d1 is before d2")
else:
    print("d1 is after d2")
```

## Output:

```plaintext
d1 is before d2
```

## 9. Example — Days Until an Event

```python
from datetime import date

today = date.today()
event_date = date(2025, 12, 31)
remaining = event_date - today

print("Days until New Year:", remaining.days)
```

## Output:

```plaintext
Days until New Year: 83
```

## 10. Example — Get Current Time in 12-Hour Format

```python
from datetime import datetime

now = datetime.now()
print(now.strftime("Current Time: %I:%M:%S %p"))
```

## Output:

```plaintext
Current Time: 10:45:22 AM
```

### Summary

| Function/Feature | Description |
| --- | --- |
| `datetime.now()` | Get current date and time |
| `datetime.today()` | Current local date |
| `timedelta(days=1)` | Time difference |
| `strftime()` | Format datetime → string |
| `strptime()` | Parse string → datetime |
| `timezone.utc` | Handle UTC time |


---

Original Source: https://www.mindstick.com/forum/161951/explain-the-python-datetime-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
