≡ Menu

How to get date / time in batch file

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

@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

Current time is 08/12/2015:22:57:24.62

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

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.

The current date is: Sat 05/16/2015
Enter the new date: (mm-dd-yy)

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


c:\>for /F "tokens=2" %i in ('date /t') do echo %i

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

The current time is: 23:17:18.57
Enter the new time:

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%

Get date and time

c:\>echo  %date%-%time%
Sun 05/17/2015-23:21:03.34
11 comments… add one
  • sonu

    Sir I want to get all outdated drivers in our pc through command prompt please help and

  • MMW

    That last part is so helpful and outstanding! Thank you so much!! :D

  • Jaganath

    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

  • jake heilbroun

    these comments were helpful but how do you make a real time updating clock in 12 hour format in a batch file?

  • Pakistani

    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.

    • Sauce

      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:
      for 8:03am

  • I need to get the files based on current date. How ya the script look like. Can someone assist me

  • markus

    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 !

  • pravin

    how to output date /T and time /T?
    like sun 1/1/2017 3:52 PM

  • Julia

    Hello, I would need a batch file, with does the Reset function in Date and Time/Change Date and Time/change calendar settings/Reset.

  • Rafeeq Moosagie

    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

Leave a Comment