Datetime

3 snippets across 2 stacks — HTML & CSS, Python

HCHTML & CSS

Date & Time Inputs

HC · Forms
syntax
<input type="date|time|datetime-local|month|week">
example
<label for="start-date">Start Date:</label>
<input type="date" id="start-date" name="startDate" min="2026-01-01" max="2026-12-31">

<label for="meeting-time">Meeting Time:</label>
<input type="datetime-local" id="meeting-time" name="meetingTime">

Note Date picker appearance varies significantly across browsers and platforms. The value format is always ISO 8601 (YYYY-MM-DD) regardless of display format. Use min and max to constrain the selectable range.

<time> Machine-Readable Dates

HC · Semantic Elements
syntax
<time datetime="YYYY-MM-DD">display text</time>
example
<p>Published on <time datetime="2026-03-15">March 15, 2026</time></p>
<p>Event starts at <time datetime="2026-04-10T09:00">9:00 AM, April 10</time></p>

Note The datetime attribute provides a machine-readable format for search engines and tools, while the visible text can use any human-friendly format.

PYPython

datetime Module

PY · Common Standard Library
syntax
from datetime import datetime, date, timedelta
example
from datetime import datetime, timedelta

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

deadline = now + timedelta(days=7, hours=3)
print(f"Due: {deadline:%B %d, %Y}")

parsed = datetime.strptime("2026-04-04", "%Y-%m-%d")
print(parsed.date())
output
2026-04-04 14:30
Due: April 11, 2026
2026-04-04

Note For timezone-aware datetimes, use datetime.now(tz=timezone.utc) instead of datetime.utcnow() which is naive and deprecated since 3.12.