Skip to content

How to Compare Two Date/Time Timestamps in Batch Script

Learn how to reliably compare timestamps in Windows Batch scripts by converting them to sortable YYYYMMDDHHmm format to avoid lexicographical errors and handle backup logic, retention policies, and process sequencing.

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

  1. Extract Year (Y), Month (M), Day (D), Hour (H), and Minute (m).
  2. Combine them into a single string: YYYYMMDDHHmm.
  3. Compare the resulting strings using standard IF statements (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

Why Compare Timestamps?

  1. Backup Logic: Comparing the "Last Modified" time of a local file vs. a remote file to see if an update is needed.
  2. Retention Policies: Checking if a log file's timestamp is less than X date to determine if it should be deleted.
  3. Process Sequencing: Ensuring that Task B only starts if Task A finished after a specific deadline.

Important Considerations

  1. Leading Zeros: You MUST ensure months, days, and hours have leading zeros (e.g., 09 instead of 9). Without them, the strings will have different lengths and lexicographic ordering will break (e.g., 9 > 10 in string comparison).
  2. 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 IF comparison performs lexicographic ordering, which works identically to numeric ordering for fixed-length numeric strings.
  3. 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"

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.

Navigation