How to Compare Two Date/Time Timestamps in Batch Script
Determining which of two dates is earlier or later is a fundamental task for building backup scripts, cleanup automation, and update checkers. However, comparing strings like 12/25/2023 to 01/10/2024 is impossible using standard lexicographical sorting because "12" is larger than "01," even though the second date is newer. To compare dates reliably, you must convert them into a Sortable Format (YYYYMMDDHHMMSS).
In this guide, we will demonstrate how to compare timestamps by transforming them into long integers.
The Strategy: The Numerical Timestamp
- Extract Year (Y), Month (M), Day (D), Hour (H), and Minute (m).
- Combine them into a single string:
YYYYMMDDHHmm. - Compare the resulting strings using standard
IFstatements (GTR,LSS).
Implementation Script
@echo off
setlocal
:: 1. Define two date strings (Assume YYYY-MM-DD HH:mm)
set "timeA=2023-12-25 14:30"
set "timeB=2024-01-10 09:00"
:: 2. Create Sortable Strings (Remove separators)
:: Result A: 202312251430
:: Result B: 202401100900
set "sortA=%timeA:-=%"
set "sortA=%sortA: =%"
set "sortA=%sortA::=%"
set "sortB=%timeB:-=%"
set "sortB=%sortB: =%"
set "sortB=%sortB::=%"
echo Comparing:
echo Time A: %sortA%
echo Time B: %sortB%
echo.
:: 3. Compare as strings (quoted variables trigger lexicographic comparison)
:: Numeric comparison would overflow, these 12-digit values exceed
:: the 32-bit integer limit (~2.1 billion / 10 digits).
if "%sortA%" GTR "%sortB%" (
echo [RESULT] Time A is NEWER than Time B.
) else if "%sortA%" LSS "%sortB%" (
echo [RESULT] Time A is OLDER than Time B.
) else (
echo [RESULT] Both timestamps are IDENTICAL.
)
pause
endlocal
When both sides of an IF comparison are wrapped in quotes, CMD performs lexicographic (string) comparison rather than numeric comparison. This is essential here because the 12-digit sortable timestamps (e.g., 202312251430) exceed the 32-bit signed integer limit of 2,147,483,647. Lexicographic comparison produces correct results as long as all strings are the same length with leading zeros preserved.
Why Compare Timestamps?
- Backup Logic: Comparing the "Last Modified" time of a local file vs. a remote file to see if an update is needed.
- Retention Policies: Checking if a log file's timestamp is less than
Xdate to determine if it should be deleted. - Process Sequencing: Ensuring that Task B only starts if Task A finished after a specific deadline.
Important Considerations
- Leading Zeros: You MUST ensure months, days, and hours have leading zeros (e.g.,
09instead of9). Without them, the strings will have different lengths and lexicographic ordering will break (e.g.,9>10in string comparison). - String Comparison, Not Numeric: The sortable timestamps in this script are 12 digits long, which exceeds the 32-bit integer limit (~10 digits). The quoted
IFcomparison performs lexicographic ordering, which works identically to numeric ordering for fixed-length numeric strings. - Local vs UTC: If your timestamps come from different sources, ensure they are in the same timezone before comparing.
Pro Tip: File Age Comparison (Native Command)
If you specifically want to find files older than a certain number of days, use the built-in forfiles command instead of manual math:
:: Find files in current dir modified MORE than 30 days ago
forfiles /P . /M *.* /D -30 /C "cmd /c echo @file is old"
forfiles handles all the date-math internally and works regardless of regional settings. It is the preferred approach when you only need to filter files by age rather than compare two arbitrary timestamps.
Conclusion
Comparing timestamps effectively requires moving beyond the visual format and into the logical format. By transforming dates and times into sortable numerical strings (YYYYMMDD), you enable your Batch scripts to make accurate chronological decisions. This ability to determine "Before" and "After" is the cornerstone of robust system maintenance and sophisticated automated workflows.