Getting Date and Time

Maintained on

When you create log files in a batch file or add a date to a backup folder, knowing how to get the current date and time is very useful.

This article covers how to use %DATE% and %TIME% and how to apply them to file names.

To get a string like 20261010_143045 regardless of regional settings, calling PowerShell is the most reliable method:

for /f %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set STAMP=%%I

Slicing %DATE% is quick, but the examples below assume the Japanese default regional setting (YYYY/MM/DD). Adjust the offsets if your format differs.

Getting the Date (%DATE%)

The %DATE% environment variable returns the current date.

echo %DATE%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %DATE%
2026/03/11
C:\users\user>
About the %DATE% format

The output format of %DATE% depends on the Windows regional settings. It is typically YYYY/MM/DD on a Japanese system, but some environments include the day of the week (for example, Wed 03/11/2026 in en-US).

Getting the Time (%TIME%)

The %TIME% environment variable returns the current time.

echo %TIME%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %TIME%
14:30:45.23
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

The %TIME% format

%TIME% is displayed as HH:MM:SS.mm. In the morning hours a leading space may appear (for example, 9:05:30.12).

Building a File-Name-Safe String from the Date

File names cannot contain slashes / or colons :, so you need to strip these characters from the date and time.

Format the Date as YYYYMMDD

@echo off
setlocal

rem When %DATE% is in YYYY/MM/DD format
set YYYY=%DATE:~0,4%
set MM=%DATE:~5,2%
set DD=%DATE:~8,2%
set TODAY=%YYYY%%MM%%DD%

echo Today's date: %TODAY%

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dateformat.cmd
Today’s date: 20260311
C:\users\user>

Format the Time as HHMMSS

@echo off
setlocal

rem Replace the leading space in the hour with 0
set HOUR=%TIME:~0,2%
set HOUR=%HOUR: =0%
set MIN=%TIME:~3,2%
set SEC=%TIME:~6,2%
set NOW=%HOUR%%MIN%%SEC%

echo Current time: %NOW%

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>timeformat.cmd
Current time: 143045
C:\users\user>
チェック

When the hour in %TIME% is a single digit (such as 9 a.m.), a leading space is included. Replacing the space with 0 using set HOUR=%HOUR: =0% makes it always two digits.

Using the Date and Time in File Names

Backup with a Timestamp

@echo off
setlocal

set YYYY=%DATE:~0,4%
set MM=%DATE:~5,2%
set DD=%DATE:~8,2%
set TIMESTAMP=%YYYY%%MM%%DD%

set BACKUP_DIR=D:\Backup\%TIMESTAMP%

mkdir "%BACKUP_DIR%" 2>nul
xcopy "C:\Users\user\Documents\*.*" "%BACKUP_DIR%\" /E /Y >nul

echo Backup destination: %BACKUP_DIR%
echo Backup completed.

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>backup_with_date.cmd
Backup destination: D:\Backup\20260311
Backup completed.
C:\users\user>

Log File with a Timestamp

@echo off
setlocal

set YYYY=%DATE:~0,4%
set MM=%DATE:~5,2%
set DD=%DATE:~8,2%
set LOGFILE=C:\Logs\process_%YYYY%%MM%%DD%.log

echo %DATE% %TIME% - Processing started. >> "%LOGFILE%"

rem Processing...
echo %DATE% %TIME% - Copied the files. >> "%LOGFILE%"

echo %DATE% %TIME% - Processing finished. >> "%LOGFILE%"

echo Log file: %LOGFILE%
endlocal

Getting the Date Independent of Regional Settings

Because %DATE% depends on regional settings, a different method is safer for batch files you distribute to other PCs.

Get-Date -Format lets you specify the format directly, so no string slicing is needed.

@echo off
setlocal

for /f %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set STAMP=%%I

echo For file names: %STAMP%

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>psdate.cmd
For file names: 20261010_143045
C:\users\user>

Starting PowerShell takes roughly half a second, so avoid calling it many times in a loop. Get the value once at the start and reuse it.

Using wmic (Older Environments)

@echo off
setlocal

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set DATETIME=%%I

set YYYY=%DATETIME:~0,4%
set MM=%DATETIME:~4,2%
set DD=%DATETIME:~6,2%
set HH=%DATETIME:~8,2%
set MIN=%DATETIME:~10,2%
set SS=%DATETIME:~12,2%

echo For file names: %YYYY%%MM%%DD%_%HH%%MIN%%SS%

endlocal
wmic may not be available on newer Windows

The wmic command is deprecated and is disabled by default (as a Feature on Demand) in Windows 11 24H2 and later and is slated for removal. For new batch files, choose the PowerShell method.

Practical Example: Automatically Deleting Old Files

Here is a batch file that deletes backup files older than 30 days.

@echo off
setlocal

set BACKUP_DIR=D:\Backup\Logs
set DAYS=30

echo Deleting files older than %DAYS% days.

forfiles /P "%BACKUP_DIR%" /S /M *.log /D -%DAYS% /C "cmd /c echo Deleting: @path && del @path" 2>nul

echo Cleanup completed.
endlocal
チェック

The forfiles command selects and processes files based on their modification date. /D -30 targets files last modified 30 or more days ago, and /S includes subfolders. For deletion tasks, first replace del @path with echo @path to check the targets before running it for real.

Summary

OperationMethod
Get the date%DATE%
Get the time%TIME%
YYYYMMDD format%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
Locale-independent datepowershell -Command "Get-Date -Format yyyyMMdd"

Practice Quiz

Practice Problem

Which statement about the %DATE% environment variable is correct?

回答がサーバーに送信されることはありません
#Command Prompt #Batch File #Date #Time #Log