git precommit hook for correcting YYYY-MM-DD strings to current date
npm install fix-ai-timeA git pre-commit hook that automatically updates dates (YYYY-MM-DD) in modified lines to the current date.
For some projects, any new date added to docs can be safely assumed to be intended to be a version of 'now' that aligns with the time the code was committed.
If your changelog or 'last updated' field is being autogenerated by AI, your timestamp is likely to be wrong. This is a simple hack to make sure it's always corrected on commit.
fix-ai-time finds dates in newly modified lines and updating them to the current date.
This is a dumb hack with a significant limitation: it will update ALL dates in any modified lines to today's date, even if those dates are legitimate historical dates that shouldn't be changed.
For example, if you edit a line containing:
``markdown`
Project started: 2020-01-01, Last updated: 2025-01-01
Both dates will be changed to today's date, even though the start date should have been preserved.
Only use this if:
1. You're specifically fixing AI-generated dates
2. You're certain that any dates in the lines you're modifying should be today's date
3. You've reviewed the changes before committing
Future versions will be smarter about which dates to update.
1. Install the package:
`bash`
npm install --save-dev fix-ai-time
2. Create the git hooks directory:
`bash`
mkdir -p .husky
3. Create the pre-commit hook:
`bash
echo '#!/bin/sh
chmod +x .husky/pre-commit
`
4. Configure git to use the hooks:
`bash`
git config core.hooksPath .husky
When you make a commit, fix-ai-time:
1. Examines the git diff of staged changes
2. Finds YYYY-MM-DD dates in newly added or modified lines
3. Updates any dates that are in the past to today's date
4. Automatically stages the changes
For example, if an AI generated this line:
`markdown`
Last updated: 2024-03-20 # AI using old training data
It will automatically become:
`markdown`
Last updated: 2025-01-03 # Today's actual date
Only dates in modified lines are updated, and only if they're in the past. Future dates are assumed to be intentional and are preserved.
Create a fix-ai-time.config.js in your project root:
`javascript`
export default {
// File extensions to process
fileExtensions: ['.md', '.mdx', '.html', '.js', '.jsx', '.ts', '.tsx'],
// Custom date pattern (default: YYYY-MM-DD)
datePattern: /\b\d{4}-\d{2}-\d{2}\b/g,
// Include time in timestamps (default: false)
includeTime: true,
// Customize the timestamp format
formatTimestamp: () => {
const now = new Date();
return {
date: now.toISOString().split('T')[0],
time: now.toISOString().split('T')[1].split('.')[0]
};
}
}
- fileExtensions: Array of file extensions to processdatePattern
- : Regular expression to match datesincludeTime
- : Whether to include time in the timestamp (default: false)formatTimestamp
- : Function that must return an object with:date
- : String in YYYY-MM-DD formattime
- : String in HH:mm:ss format
Example valid formatTimestamp:
`javascript`
formatTimestamp: () => ({
date: '2024-03-21', // Must be YYYY-MM-DD
time: '14:30:00' // Must be HH:mm:ss
})
When includeTime is true, dates will be formatted as YYYY-MM-DDTHH:mm:ss.
By default, fix-ai-time processes files with these extensions:
- .md
- .mdx
- .html
- .js
- .jsx
- .ts
- .tsx
To skip the hook for a particular commit:
`bash``
git commit --no-verify
MIT
Issues and pull requests welcome!