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?
migrated from stackoverflow.com 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:
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.
We need a few leading zeroes:
Then it's time to assemble the parts:
(Just a random note: Why on earth would you want that timestamp format?)
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:
If you want to see all possible formating patterns simply call
Please take care to include the ".exe" extension. If you leave it off then windows will call the internal date command.
I use variations of this block of code in nearly every .bat file I write.
Use "%datestr%" to get your date string, i.e.:
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,