How to Convert a Timestamp to a Readable Date/Time in Batch Script
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
Unix Epoch timestamps are measured in UTC. The .LocalDateTime property automatically converts the result to your system's local timezone. To display the result in UTC instead, use .UtcDateTime:
powershell -NoProfile -Command "[DateTimeOffset]::FromUnixTimeSeconds(%epoch%).UtcDateTime.ToString('yyyy-MM-dd HH:mm:ss')"
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
Windows FILETIME values are 64-bit integers that far exceed Batch's 32-bit limit. The value is passed to PowerShell as a string within the command and is never processed by Batch's set /a arithmetic. PowerShell's [DateTime]::FromFileTime() handles the full 64-bit range natively.
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
wmic is deprecated in newer versions of Windows. As a future-proof alternative, you can retrieve the same formatted date string using PowerShell:
for /f "usebackq delims=" %%I in (`
powershell -NoProfile -Command "Get-Date -Format 'yyyy-MM-dd HH:mm:ss'"
`) do set "readable=%%I"
The WMIC method (Method 3) only converts the current system date and time. It cannot convert arbitrary timestamps. For converting stored or historical timestamps, use Method 1 (Epoch) or Method 2 (FILETIME).
Why Convert Timestamps?
- Log Analysis: Server logs and event viewers often store events as Epoch timestamps. Converting them makes the data human-readable.
- Auditing: Checking when a file was last modified, a user last logged in, or a certificate was issued.
- Debugging: When comparing timestamps from different systems (Unix, Windows, databases), having them all in a readable format prevents confusion.
Important Considerations
If you need to convert many Epoch timestamps in a loop, you can batch them into a single PowerShell call to avoid the overhead of launching PowerShell repeatedly:
@echo off
setlocal enabledelayedexpansion
set "epoch1=1703512800"
set "epoch2=1703599200"
set "epoch3=1703685600"
for /f "usebackq delims=" %%R in (`
powershell -NoProfile -Command ^
"$epochs = @(%epoch1%, %epoch2%, %epoch3%); foreach ($e in $epochs) { [DateTimeOffset]::FromUnixTimeSeconds($e).LocalDateTime.ToString('yyyy-MM-dd HH:mm:ss') }"
`) do echo %%R
pause
PowerShell's .ToString() method accepts any .NET date format string. Common alternatives include:
| Format String | Example Output |
|---|---|
'yyyy-MM-dd HH:mm:ss' | 2023-12-25 14:00:00 |
'dd/MM/yyyy' | 25/12/2023 |
'MMM dd, yyyy h:mm tt' | Dec 25, 2023 2:00 PM |
'yyyyMMdd_HHmmss' | 20231225_140000 |
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.