This post explains how to get current date and time from command prompt or in a batch file.
How to get date and time in a batch file
Below is a sample batch script which gets current date and time
Datetime.cmd
@echo off for /F "tokens=2" %%i in ('date /t') do set mydate=%%i set mytime=%time% echo Current time is %mydate%:%mytime%
When we run the above batch file
C:\>datetime.cmd Current time is 08/12/2015:22:57:24.62 C:\>
Get date from command line
To print today’s date on the command prompt, we can run date /t
.
c:\>date /t Thu 05/14/2015 c:\>
Just running date
without any arguments prints the current date and then prompts to enter a new date if the user wants to reset it.
c:\>date The current date is: Sat 05/16/2015 Enter the new date: (mm-dd-yy) c:\>
In addition to date command, we also have an environment variable using which we can find today’s date.
c:\>echo %date% Sun 05/17/2015
How to get only the date in MM/DD/YYYY format?
You may want to exclude the day (like ‘Sun’ in the above example) and print only the date in MM/DD/YYYY format. The below command works for the same.
for /F "tokens=2" %i in ('date /t') do echo %i
Example:
c:\>for /F "tokens=2" %i in ('date /t') do echo %i 05/14/2015 c:\>
Get time from command prompt
Similar to date command, we have the command time
which lets us find the current system time. Some examples below.
c:\>time /t 11:17 PM c:\>time The current time is: 23:17:18.57 Enter the new time: c:\>
As you can see, the command prints the time in different formats. It prints in 12 hour format when /t
is added and in 24 hours format without /t
We can also get the current time from environment variables.
c:\>echo %time% 23:23:51.62 c:\>
Get date and time
c:\>echo %date%-%time% Sun 05/17/2015-23:21:03.34
Sir I want to get all outdated drivers in our pc through command prompt please help and
reply
Thanks
That last part is so helpful and outstanding! Thank you so much!! :D
-Matthew
I want the last week date from the current date :-This is the script,I am using for the getting the current date.
for /F “tokens=2” %i in (‘date /t’) do echo %i
05/14/2015
these comments were helpful but how do you make a real time updating clock in 12 hour format in a batch file?
Create a file called realtimeClock.bat.
This is the contents of realtimeClock.bat:
——
@echo off
:getTime
echo The current time is %time%
cls
goto :getTime
——
Run, and enjoy.
Excellent guideline. If the hour (time) is less than 10 then %time% return a space before the hour, so I prefer to use the ‘time /t’ approach.
You can solve that by:
echo %TIME: =0%
(there’s a space between : and =). That will replace the space with a 0
Let’s say you wanted to use the variable to create a filename or log based on the current time:
rem Extract the hour and minute from the time
set TM=%TIME:~0,2%%TIME:~3,2%
rem Zero-pad the hour if it is before 10am
set TM=%TM: =0%
echo %TM%
output is:
0803
for 8:03am
I need to get the files based on current date. How ya the script look like. Can someone assist me
how to do this with date and time of a file ?
Why doesn’t it work with “ftime, fdate” ?
please any hint how to do this would be great !
Reply to markus’ question: How to do this with date and time of a file ?
To read the date+time of a file, call DIR in a FOR loop, like so:
FOR /F “tokens=1,2,3,4,*” %%a in (‘DIR “filename.ext”/4 ^| find “/”‘) do set “filedatetime=%%a %%b %%c” & set “filesize=%%d” & set “filename=%%e” & REM Do whatever you want here
Note: There is an apostrophe (single-quote) between the double-quote and the right-paren.
You can, of course, use any switches you want in the DIR command to refine your selection criteria. Type DIR /? for more info.
The FIND command filters the output of DIR to eliminate the header and footer. (DIR /B only lists the filenames, not the dates.)
“tokens=1,2,3,4,*” parses the output into separate variables. The asterisk at the end puts the entire filename (including spaces) into the fifth variable.
how to output date /T and time /T?
like sun 1/1/2017 3:52 PM
Hello, I would need a batch file, with does the Reset function in Date and Time/Change Date and Time/change calendar settings/Reset.
good day
Is it possible to get the date and then use it as an input string in a for loop so that the day can increment
Thank you
I need a .bat script to change the system date one day ahead, (change it to tomorrow’s date). That’s it.
Solutions above break if locale changes. See: https://serverfault.com/questions/227345/locale-unaware-date-and-time-in-batch-files
Agreed. The format is dependent on Windows Regional Settings and more specifically the date and time formats however … this article does an exceptional job of explaining that all, and how to “Grab” the individual components of (in the above example) of the date.
What I ended up with:
@Echo off
for /F “tokens=1” %%i in (‘date /t’) do set myday=%%i
for /F “tokens=2” %%i in (‘date /t’) do set mymonth=%%i
for /F “tokens=3” %%i in (‘date /t’) do set myyear=%%i
set mytime=%time%
MD E:\Daily_Report\%myyear%
MD E:\Daily_Report\%myyear%\%mymonth%
echo Current time is %myday%:%mymonth%:%myyear%: :%mytime% > E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
ping 192.168.xxx.xxx >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
ping 8.8.8.8 >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
ping http://www.google.com >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
Echo. >> E:\Daily_Report\%myyear%\%mymonth%\%myday%.txt
[end] – Hope this helps somebody. Unfortunately there seems to be no way to tell the date or time commands what format one wants returned. It seems dependent on your regional settings for that specific windows installation.
Thanks to the author, this article certainly dusted off some old cobwebs.
hello
I need to run a file in a certain date without using wndows Task Scheduler.
how can I do that using batch file?
please help me.
I fear you will end up with a protected mode shell running all the time. This would certainly not be my preference.
Your Batch file would essentially be a loop, looking if the date required has been achieved. If the value comparison if true, it would execute (possibly a sub:) and then return to the start of the loop.
This could also be used in connection with a data “Read” to input a sequence of dates with respective sub routines that are called for the respective date “triggers”
Hope this helped
hello
I need to run a file in a certain date without using windows Task Scheduler.
how can I do that using batch file?
please help me.
please answer me through my G-mail
thanks alot
All of the above scripts which call both time and date (or %time% and %date%) suffer from one minor problem: time and date are called separately, so if the script runs just before midnight (a very narrow window, to be sure – at most a few milliseconds), the date could roll over between the two calls and your result would be 24 hours early (i.e. just after midnight on the date that the command started).
Here are 2 solutions:
1.
for /f “tokens=2 delims==” %%I in (‘wmic os get localdatetime /format:list’) do set datetime=%%I
set datetime=%datetime:~0,8%-%datetime:~8,6%
wmic gets the date + time in an atomic operation, so no rollover is possible.
The second line formats datetime in the form I needed for what I used it for. You can modify this line to format however suits your needs.
2.
:timeloop
set mydate=%date%
set mytime=%time%
if mydate NEQ %date% goto :timeloop
If the date rolls over between the two calls to %date%, just go back and try again.
Hi! Great information here. I am in need of a way to get the full 24 hr time displayed live. I need it to show the milliseconds that you show above in your example:
c:\>echo %time%
23:23:51.62
in command promt if i keep hitting up arrow then enter it will display the new time. I am looking for a way to make that happen automatically without me hitting enter constantly. Thanks in advance!
Thank you very much Srini for this tutorial. I created my own Windows 10 Defender Virus scan batch command and used schedule task to run it and used your commands to create log files with date and time stamp.
Very helpful! Thanks a lot!
Hi everyone!
i did the first example in w10 pro but it didn’t work. print the following information. So, the command “date /t” doesn’t print in the output. any idea why? i need to print date and time every time when apply a wifi command to know if the BSSID has changed.
@echo off
for /F “tokens=2” %%i in (‘date /t’) do set mydate=%%i
set mytime=%time%
echo Current time is %mydate%:%mytime%
C:\>timestamp_2.bat
Current time is :11:35:57,87
Dear Sir
I have an idea,
Can you get the date and time string when you click and drag the bottom right clock of the windows taskbar, drag it to any filename box?
Thanks
I’m trying to understand the first example. I’m new to batch scripting.
@echo off
for /F “tokens=2” %%i in (‘date /t’) do set mydate=%%i
set mytime=%time%
echo Current time is %mydate%:%mytime%
The first token is stored in %%i and then mydate is set to that value. Correct? Where does the variable ‘time’ come from? Wouldn’t that second token be stored inside of ‘%%j’? Also, why can’t I store the entire value of ‘date /t’ in a variable and print/echo that? It doesn’t seem to work.
How would I get the date into a variable for an actual, valuable use?
Remove the slashes in the date.
Remove the leading space from the time. (or right-justify it with 0s)
Change the :s into ;s for the time.
Something that I can use to create a filename:
MyFile-2021-12-31T09;59;59.txt
The DATE cmd would be infinitely more valuable with a /F option that
outputs an ISO standard date format
Awesome! Thanks.
Hope following commands can help to extract date and time
set “extractdate=%date:~-7,2%%date:~-10,2%%date:~-4,4%”+’000000′
set “extracttime=%time:~-12,2%%time:~-8,2%%time:~-5,2%
echo %extractdate%%extracttime%
I am using same formula to get date as variable from the current local machine date, but it gives me error of invalid number from august month as this script was running well till 31st july.