Back to catalogue
ContextUserPromptSubmit
Current date and time injection
No more answers stuck on last year's date
Adds the current date and time to the context of every prompt, preventing the agent from reasoning with an incorrect cutoff date.
What does the Current date and time injection hook do?
Current date and time injection is a Claude Code UserPromptSubmit hook. It fires automatically at that lifecycle event — outside the model, so it can't be skipped or forgotten. No more answers stuck on last year's date.
As a UserPromptSubmit hook it runs before the action completes, so it can block or adjust what Claude is about to do. Because it is a deterministic Node.js script, it executes on every matching event without relying on the model to remember — the guarantee that makes agentic workflows safe to automate.
Use cases
- Correct temporal reasoning
- Timestamped logs
- Changelogs
Tags
#context#datetime#prompt
settings.json fragment
{
"hooks": {
"UserPromptSubmit": [
{
"hooks": [
{
"command": "echo \"Date/heure actuelle: $(date '+%Y-%m-%d %H:%M:%S %Z')\"",
"type": "command"
}
]
}
]
}
}Script · .claude/hooks/inject-datetime.mjs
#!/usr/bin/env node
// @hookstack user-prompt-inject-datetime
// Injecte la date et l'heure courantes dans chaque prompt (UserPromptSubmit)
import { fileURLToPath } from "node:url";
export function run({ now = new Date() } = {}) {
const formatted = now.toLocaleString("fr-FR", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
timeZoneName: "short",
});
return `Date et heure courantes : ${formatted}\n`;
}
/* v8 ignore next 3 */
if (process.argv[1] === fileURLToPath(import.meta.url)) {
process.stdout.write(run());
}