Skip to content

How to Convert a Timestamp to a Readable Date/Time in Batch Script

Learn how to convert Unix Epoch timestamps (seconds since 1970) and Windows FILETIME values (100-nanosecond intervals since 1601) into human-readable dates in Batch scripts using PowerShell.

Many systems store timestamps as a single long number, either a Unix Epoch (seconds since 1970-01-01) or a Windows FILETIME (100-nanosecond intervals since 1601-01-01). Converting these raw integers back into a human-readable "YYYY-MM-DD HH:MM:SS" format is essential for log analysis, auditing, and debugging.

In this guide, we will demonstrate how to convert a Unix Epoch timestamp to a readable date using PowerShell, and how to handle Windows FILETIME values.

Method 1: Unix Epoch to Readable Date (PowerShell Bridge)

PowerShell provides the most reliable way to convert an Epoch timestamp.

Implementation Script

@echo off
setlocal

:: Example Unix timestamp: 1703512800 (2023-12-25 14:00:00 UTC)
set "epoch=1703512800"

for /f "usebackq delims=" %%A in (`
    powershell -NoProfile -Command "[DateTimeOffset]::FromUnixTimeSeconds(%epoch%).LocalDateTime.ToString('yyyy-MM-dd HH:mm:ss')"
`) do set "readable=%%A"

echo.
echo ==========================================
echo EPOCH:    %epoch%
echo READABLE: %readable%
echo ==========================================
pause

Method 2: Windows FILETIME to Readable Date

Windows sometimes stores dates as FILETIME (a 64-bit value). PowerShell can handle this conversion as well:

@echo off
setlocal

:: Example: FILETIME value
set "filetime=133479360000000000"

for /f "usebackq delims=" %%A in (`
    powershell -NoProfile -Command "[DateTime]::FromFileTime(%filetime%).ToString('yyyy-MM-dd HH:mm:ss')"
`) do set "readable=%%A"

echo.
echo FILETIME: %filetime%
echo READABLE: %readable%
pause

Method 3: WMIC DateTime to Readable Date (Pure Batch)

The wmic command returns dates in a long format like 20231225140000.000000+060. You can slice this string into a readable format:

@echo off
setlocal enabledelayedexpansion

:: Get current datetime from WMIC
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do (
    set "dt=%%I"
)

:: Validate that dt is not empty
if not defined dt (
    echo [ERROR] Failed to retrieve system date.
    pause
    exit /b
)

:: Slice the string into readable format
set "readable=!dt:~0,4!-!dt:~4,2!-!dt:~6,2! !dt:~8,2!:!dt:~10,2!:!dt:~12,2!"

echo Formatted: !readable!
pause

Why Convert Timestamps?

  1. Log Analysis: Server logs and event viewers often store events as Epoch timestamps. Converting them makes the data human-readable.
  2. Auditing: Checking when a file was last modified, a user last logged in, or a certificate was issued.
  3. Debugging: When comparing timestamps from different systems (Unix, Windows, databases), having them all in a readable format prevents confusion.

Important Considerations

Conclusion

Converting raw timestamps to readable dates is a critical "translation" skill for any system administrator. By leveraging PowerShell's built-in date conversion methods, you can decode any timestamp format, Epoch, FILETIME, or WMIC, into a clear, standardized output. This capability is essential for log analysis, security auditing, and cross-system date synchronization.

Navigation