Need to find the beginning of a time period for date range queries or reporting? This guide shows you how to use the Start of Day API to get the exact start of any day, week, month, quarter, or year with proper timezone handling.
What is Start of Day Calculation?
Start of day (or start of period) calculation finds the beginning boundary of a time period containing a given date. For example, the start of day for "2024-01-15 14:30:00" is "2024-01-15 00:00:00".
This is essential for creating date range queries, generating reports, and ensuring consistent time boundaries across your application.
Supported Time Units
The API can find the start of the following periods:
- Second: Start of the current second (truncates milliseconds)
- Minute: Start of the current minute (00 seconds)
- Hour: Start of the current hour (00:00 seconds)
- Day: Start of the current day (00:00:00)
- Week: Start of the current week (configurable first day)
- Month: First day of the current month
- Quarter: First day of the current quarter
- Year: First day of the current year (January 1st)
Using the Start of Day API
TinyFn provides a simple endpoint for start of period calculations:
GET https://api.tinyfn.io/v1/time/start-of
Headers: X-API-Key: your-api-key
{
"original": "2024-01-15T14:30:45.123Z",
"result": "2024-01-15T00:00:00.000Z",
"unit": "day",
"timezone": "UTC"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
date |
string | Date in ISO 8601 format (required) |
unit |
string | Unit: second, minute, hour, day, week, month, quarter, year (required) |
timezone |
string | IANA timezone (default: UTC) |
week_start |
string | First day of week: sunday or monday (default: sunday) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/time/start-of?' + new URLSearchParams({
date: '2024-01-15T14:30:00Z',
unit: 'month'
}),
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const result = await response.json();
console.log(result.result); // 2024-01-01T00:00:00.000Z
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/time/start-of',
params={
'date': '2024-03-15T10:00:00Z',
'unit': 'quarter'
},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['result']) # 2024-01-01T00:00:00.000Z (Q1 starts Jan 1)
cURL
curl "https://api.tinyfn.io/v1/time/start-of?date=2024-01-15&unit=week&week_start=monday" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Date Range Queries: Get the start of a period for database queries
- Report Generation: Define report boundaries (start of month, quarter, year)
- Analytics Dashboards: Group data by day, week, or month
- Billing Cycles: Calculate billing period start dates
- Calendar Applications: Display week or month views
Best Practices
- Always specify timezone: Start of day varies by timezone
- Use with end-of: Pair with end-of-day API for complete date ranges
- Consider DST: Be aware of daylight saving time transitions
- Document week start: Make clear whether your app uses Sunday or Monday as week start
Try the Start of Day API
Get your free API key and simplify your date boundary calculations.
Get Free API Key