Back to catalogue
ContextUserPromptSubmitUserPromptSubmitOn prompt submit · can enrich input⚡ blocking
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.
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
// Injecte la date et l'heure courantes dans chaque prompt (UserPromptSubmit)
import { fileURLToPath } from '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());
}