Overview
LLMs do not know the current clock or user timezone unless you inject them into the prompt. Passing a structured temporal block with the exact timestamp, IANA timezone, and UTC offset prevents date and day-of-week mistakes.
- Anchor relative dates to a known clock: Words like “today,” “tomorrow,” or “next Friday” only resolve accurately when the prompt states the exact current date, day, and timezone.
- Separate display time from UTC storage: Giving both local time and ISO-8601 UTC makes scheduling, reminders, and database queries unambiguous.
Steps
- Resolve the user’s IANA timezone and current clock: Read the user’s timezone (such as
Asia/Karachi) with a safe fallback, and capture the current instant in both local time and UTC. - Format a structured temporal block: Build a clear, compact temporal context block showing current local date/time, day of week, IANA timezone, and UTC offset.
- Inject the block before user messages: Place the temporal context in the system prompt or right before the turn, and instruct the model to resolve relative dates against it.
- Parse and validate output dates in code: Convert any extracted or scheduled date/time back through a timezone-aware library so DST and boundary rules stay exact.
Example
export interface TemporalContext {
nowIsoUtc: string;
nowLocalFormatted: string;
localDateIso: string; // YYYY-MM-DD in target timezone
dayOfWeek: string;
timeZone: string;
utcOffset: string;
}
export function buildTemporalContext(
timeZone: string,
now: Date = new Date()
): TemporalContext {
const safeTimeZone = isValidTimeZone(timeZone) ? timeZone : "UTC";
const datePartsFormatter = new Intl.DateTimeFormat("en-CA", {
timeZone: safeTimeZone,
year: "numeric",
month: "2-digit",
day: "2-digit",
});
const weekdayFormatter = new Intl.DateTimeFormat("en-US", {
timeZone: safeTimeZone,
weekday: "long",
});
const fullLocalFormatter = new Intl.DateTimeFormat("en-US", {
timeZone: safeTimeZone,
dateStyle: "full",
timeStyle: "long",
});
const offsetFormatter = new Intl.DateTimeFormat("en-US", {
timeZone: safeTimeZone,
timeZoneName: "shortOffset",
});
const offsetPart =
offsetFormatter.formatToParts(now).find((p) => p.type === "timeZoneName")?.value ??
"GMT";
return {
nowIsoUtc: now.toISOString(),
nowLocalFormatted: fullLocalFormatter.format(now),
localDateIso: datePartsFormatter.format(now),
dayOfWeek: weekdayFormatter.format(now),
timeZone: safeTimeZone,
utcOffset: offsetPart,
};
}
export function isValidTimeZone(timeZone: string): boolean {
try {
Intl.DateTimeFormat(undefined, { timeZone });
return true;
} catch {
return false;
}
}
export function formatTemporalPromptBlock(ctx: TemporalContext): string {
return [
"<temporal_context>",
`current_local_datetime: ${ctx.nowLocalFormatted}`, `current_local_date: ${ctx.localDateIso}`,
`current_day_of_week: ${ctx.dayOfWeek}`, `iana_timezone: ${ctx.timeZone}`,
`utc_offset: ${ctx.utcOffset}`, `current_utc_iso: ${ctx.nowIsoUtc}`,
"</temporal_context>",
"Resolve all relative dates and times using iana_timezone and current_local_datetime above.",
].join("\n");
}
export function buildTimezoneAwareMessages(params: {
baseSystemPrompt: string;
userMessage: string;
timeZone: string;
now?: Date;
}): Array<{ role: "system" | "user"; content: string }> {
const temporalCtx = buildTemporalContext(params.timeZone, params.now);
const temporalBlock = formatTemporalPromptBlock(temporalCtx);
return [
{
role: "system",
content: `${params.baseSystemPrompt}\n\n${temporalBlock}`,
},
{
role: "user",
content: params.userMessage,
},
];
}
Tips
- Use IANA timezones, not fixed offsets alone: Fixed offsets break across Daylight Saving Time transitions, while IANA zones like
Asia/KarachiorAmerica/New_Yorkkeep exact rules. - Include the day of week explicitly: Models often miscalculate which weekday a date falls on unless you state the current day of week clearly.
- Treat LLM date strings as untrusted until parsed: Always validate resulting ISO timestamps and convert to UTC on the server before saving to a database or scheduling jobs.
You must log in or register to comment.
