How to print the current time in a Batch-File?

Batch FileTime

Batch File Problem Overview


I need to print time in a batch file but command prompt tells me that the syntax is incorrect. Here is the code i have so far:

@echo %time%
ping -n 1 -w 1 127.0.0.1 1>nul
@echo %time%
pause
cls

I don't know why it isn't working, Please help Me.

Batch File Solutions


Solution 1 - Batch File

This works with Windows 10, 8.x, 7, and possibly further back:

@echo Started: %date% %time%
.
.
.
@echo Completed: %date% %time%

Solution 2 - Batch File

If you use the command

time /T

that will print the time. (without the /T, it will try to set the time)

date /T

is similar for the date.

If cmd's Command Extensions are enabled (they are enabled by default, but in this question they appear to be disabled), then the environment variables %DATE% and %TIME% will expand to the current date and time each time they are expanded. The format used is the same as the DATE and TIME commands.

To see the other dynamic environment variables that exist when Command Extensions are enabled, run set /?.

Solution 3 - Batch File

we can easily print the current time and date using echo and system variables as below.

echo %DATE% %TIME%

output example: 13-Sep-19 15:53:05.62

Solution 4 - Batch File

Not sure if your question was answered.

This will write the time & date every 20 seconds in the file ping_ip.txt. The second to last line just says run the same batch file again, and agan, and again,..........etc.

Does not seem to create multiple instances, so that's a good thing.

@echo %time% %date% >>ping_ip.txt
ping -n 20 -w 3 127.0.0.1 >>ping_ip.txt
This_Batch_FileName.bat
cls

Solution 5 - Batch File

The simplest way using just DOS is echo. | time.

This method even works in a Windows 7 x64 Command window.

Solution 6 - Batch File

You can use the command time /t for the time and date /t for the date, here is an example:

@echo off
time /t >%tmp%\time.tmp
date /t >%tmp%\date.tmp
set ttime=<%tmp%\time.tmp
set tdate=<%tmp%\date.tmp
del /f /q %tmp%\time.tmp
del /f /q %tmp%\date.tmp
echo Time: %ttime%
echo Date: %tdate%
pause >nul

You can also use the built in variables %time% and %date%, here is another example:

@echo off
echo Time: %time:~0,5%
echo Date: %date%
pause >nul

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2975367View Question on Stackoverflow
Solution 1 - Batch FileErik AndersonView Answer on Stackoverflow
Solution 2 - Batch FilechwarrView Answer on Stackoverflow
Solution 3 - Batch FileSam CoorgView Answer on Stackoverflow
Solution 4 - Batch FileBill HardingView Answer on Stackoverflow
Solution 5 - Batch FileMartin WhittakerView Answer on Stackoverflow
Solution 6 - Batch FileHayzView Answer on Stackoverflow