Truncate datetime to unit
Round a date and time down to the start of its year, month, day, hour or minute in UTC: 2024-06-15T14:37:52Z truncated to the day becomes 2024-06-15T00:00:00Z. Useful for daily reports, hourly charts and grouping records. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Datetime
- 2024-06-15T14:37:52Z day
- Result
- 2024-06-15T00:00:00Z day
- Year
- 2,024
- Month
- 6
- Day
- 15
Truncating a timestamp to a coarser unit is the foundation of daily buckets, monthly reports, hourly charts, and cache keys that must agree across languages and hosts. Impossible calendar days and unknown units fail with a clear validation error instead of inventing a rollover.
How UTC truncation works without a system clock
Start from an explicit base datetime written in the international ISO form so every client agrees on the same year, month, day, and optional clock fields. When only a calendar date is supplied, treat it as midnight Coordinated Universal Time. When a time is present, accept optional fractional seconds truncated to whole seconds and either a trailing Z for UTC or a numeric plus-or-minus hour-minute offset that is subtracted when converting to absolute time.
Validate that the civil day exists under Gregorian leap rules—February twenty-nine is legal only when the year is divisible by four except that century years must also be divisible by four hundred—and that hour, minute, and second lie in their usual ranges. Convert the civil triple to a day serial with the Howard Hinnant days-from-civil algorithm, multiply by eighty-four thousand six hundred seconds, add the local time-of-day, and subtract offset minutes times sixty to obtain UTC epoch seconds. Reconstruct UTC year, month, day, hour, minute, and second with civil-from-days.
Format the truncated civil fields as a four-two-two T two-two-two Z string.
Where truncate-datetime-to-unit calculators earn their keep
Analytics pipelines materialize daily and hourly event keys by flooring ingestion timestamps so every language and region shares the same UTC bucket without inventing a calendar library. Finance and operations teams group ledger lines or support tickets into month or day periods for deterministic summaries that match offline audits. Product and growth dashboards chart activity by hour or day using a stable floor so browser previews and backend aggregations never disagree by one hour across daylight-saving transitions.
If you later need to add hours or days to a timestamp, count days between two dates, compute month start and end ranges, or convert pure time units between hours and minutes, treat those as separate search intents and landings rather than overloading this truncate-datetime-to-unit page—those problems solve different queries even when some arithmetic looks similar under the hood.
What you can do with it
Daily and hourly analytics buckets
Floor ISO event timestamps to day or hour in UTC so every pipeline language materializes the same aggregation key for charts and rollups.
Month and year report periods
Truncate ledger or ticket datetimes to month or year to build deterministic period starts for exports, audits, and finance summaries.
FAQ
How do I truncate a datetime to the day in UTC?
Send datetime as an ISO-8601 string and unit as day.
Which units are supported?
Exactly five case-insensitive tokens: year, month, day, hour, and minute.
Does truncation use my local timezone?
No. The floor is always computed in UTC from the explicit input string. Offsets in the string are converted to UTC first; the host clock is never read.
What happens with an invalid calendar day?
The task does not roll into March or invent a leap day.
What do I get when I truncate to the month or the year?
Truncating to the month gives the first day of that month at midnight UTC. Truncating to the year gives 1 January of that year at midnight UTC, with every smaller part set to zero.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/date/truncate-to-unit \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"datetime":"2024-06-15T14:37:52Z","unit":"day"}'const res = await fetch("https://api.kit.forhosting.com/date/truncate-to-unit", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"datetime": "2024-06-15T14:37:52Z",
"unit": "day"
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/date/truncate-to-unit",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"datetime": "2024-06-15T14:37:52Z",
"unit": "day"
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/date/truncate-to-unit", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"datetime":"2024-06-15T14:37:52Z","unit":"day"}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"datetime":"2024-06-15T14:37:52Z","unit":"day"}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/date/truncate-to-unit", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"datetime": "2024-06-15T14:37:52Z",
"unit": "day"
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "date.truncate_to_unit",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
Host time zones and daylight-saving tables make a naive floor operation diverge between browsers, servers, and edge Workers. This truncate-datetime-to-unit tool accepts an explicit ISO-8601 datetime—full clock time with Z or a numeric offset, or a date-only string treated as midnight UTC—plus a unit of year, month, day, hour, or minute. It normalizes the input to Coordinated Universal Time with pure Gregorian calendar math, then zeros every field smaller than the chosen unit so the result is a stable UTC floor instant written as year-month-day T hour-minute-second Z.
How UTC truncation works without a system clock
Then apply a pure floor by unit: year resets month to one and day to one with the clock at zero; month keeps year and month but sets day to one and the clock to zero; day keeps the calendar date and zeros the clock; hour zeros minute and second; minute zeros only the second.
What you send and what you get back
Send two required fields: datetime as an ISO-8601 string such as two thousand twenty-four hyphen zero six hyphen fifteen T fourteen colon thirty-seven colon fifty-two Z, and unit as one of year, month, day, hour, or minute (case-insensitive). Classic checks include flooring fourteen thirty-seven to the day at midnight UTC, flooring a plus-two offset morning timestamp to the hour in pure Z, flooring to the month on the first day, flooring to the year on January first, and rejecting a base such as the twenty-ninth of February in a common year. Aliases date, dt, timestamp, and ts are accepted for the base string; aliases to, truncate_to, and granularity are accepted for the unit. Missing fields, non-string datetimes, strings that do not match ISO shape, impossible calendar days, invalid time components, and units outside the five allowed tokens all fail with invalid_input and are not charged. On success the response echoes the trimmed base datetime, the normalized lowercase unit, the result UTC ISO string after truncation, the numeric year, month, day, hour, minute, and second of the floor instant, the result epoch_seconds, and a short formula string documenting the UTC floor rule. Pricing for successful API calls is the published base of $0.002 per item; validation failures do not consume prepaid balance.
Where truncate-datetime-to-unit calculators earn their keep
Because the capability is a pure parse engine, API pricing is the published base of $0.002 per successful item and failed validation does not consume balance.
Integration use cases
QA fixtures for bucket boundaries
Generate expected floor instants across midnight, month starts, and offset-to-UTC conversion for regression tests without a host clock.
Technical questions
What does the truncate datetime to unit tool cost?
The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input is not billed.