Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to get the current system date and time using a command prompt.
How do I get the date to be in a specific format, MM-DD-YYYY HH:MIN AM/PM?

share|improve this question

migrated from Apr 30 '11 at 6:04

This question came from our site for professional and enthusiast programmers.

Very easy to get the date and time, actually:

set Year=
for /f "skip=2" %%x in ('wmic Path Win32_LocalTime get Year^,Month^,Day^,Hour^,Minute^,Second /Format:List') do (
  if not defined Year set %%x

I'm assuming local time here. If you need UTC, adapt it accordingly.

Your format makes things more complicated. Apologies if I get something wrong here, I'm not familiar with am/pm formats.

if %Hour% LSS 12 (
  set ampm=AM
  if %Hour%==0 set Hour=12
) else (
  set ampm=PM
  set /a Hour-=12

We need a few leading zeroes:

if %Month% LSS 10 set Month=0%Month%
if %Day% LSS 10 set Day=0%Day%
if %Minute% LSS 10 set Minute=0%Minute%
if %Hour% LSS 10 set Hour=0%Hour%

Then it's time to assemble the parts:

set Timestamp=%Month%-%Day%-%Year% %Hour%:%Minute% %ampm%

(Just a random note: Why on earth would you want that timestamp format?)

share|improve this answer
nice solution! in some countries, "day" goes before "month" in a date string... – kokbira May 16 '11 at 17:37
but your solution functions only with admin privileges... – kokbira May 16 '11 at 17:43
kokbira: It works fine here as a normal user. – Joey May 16 '11 at 23:37
mmm. perhaps for Windows 7 it functions, but for Windows XP it doesn't with normal user – kokbira May 16 '11 at 23:41

Unix command line tools are often more powerful than their windows counterparts.

But even with Windows you can use some ported command line tools. For example the free UnxTools package:

Here's the download link:

Just take the date.exe from the archive (it's in the subfolder usr\local\wbin) and put it in a folder you also have in your PATH environment.

Then you can call the date.exe with parameters like this:

date.exe +"%m-%d-%y %l:%M %p"

If you want to see all possible formating patterns simply call

date.exe --help

Please take care to include the ".exe" extension. If you leave it off then windows will call the internal date command.

share|improve this answer
:: construct date and time strings
for /f "tokens=1,2" %%u in ('date /t') do set d=%%v 
for /f "tokens=1" %%u in ('time /t') do set t=%%u 
for /f "tokens=2" %%u in ('time /t') do set a=%%u
if "%t:~1,1%"==":" set t=0%t% 
set datestr=%d:~0,2%-%d:~3,2%-%d:~6,4% %t:~0,2%:%t:~3,2% %a%

I use variations of this block of code in nearly every .bat file I write.

Use "%datestr%" to get your date string, i.e.:

echo %datestr%

share|improve this answer
nice, but you forgot in some countries default date and time format are different (for example, in my country the default time format is 24h - without "am/pm") – kokbira May 16 '11 at 17:41

See Stack Overflow question How to get a UNIVERSAL Windows batch file timestamp for an example of how to get an date time setting that is independent of regional settings. To call the script,

c:\> cscript //nologo myscript.vbs 

Use a for loop to capture the output if needed.

share|improve this answer
i am getting the dayof the week which is not required. – sachin Apr 28 '11 at 8:09
you specified DD. Isn't that what you want? go to here:… and download the vbscript manual. For hours and min, try Hour() , minute() – user31894 Apr 28 '11 at 8:15
i require the DD value in numeric form . but it also displays it as thu which i need to delete . – sachin Apr 28 '11 at 9:18

You must log in to answer this question.