2
2
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
?
2
2
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
?
4
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?)
3
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:
http://sourceforge.net/projects/unxutils/
Here's the download link:
http://sourceforge.net/projects/unxutils/files/unxutils/current/UnxUtils.zip/download
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.
3
:: construct date and time strings
:: "MM-DD-YYYY HH:MIN AM/PM"
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%
2nice, 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 – 2011-05-16T17:41:26.427
0
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.
1i am getting the dayof the week which is not required. – None – 2011-04-28T08:09:11.830
you specified DD
. Isn't that what you want? go to here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9 and download the vbscript manual. For hours and min, try Hour() , minute()
1i require the DD value in numeric form . but it also displays it as thu which i need to delete . – None – 2011-04-28T09:18:06.450
nice solution! in some countries, "day" goes before "month" in a date string... – kokbira – 2011-05-16T17:37:50.593
but your solution functions only with admin privileges... – kokbira – 2011-05-16T17:43:27.110
kokbira: It works fine here as a normal user. – Joey – 2011-05-16T23:37:27.983
mmm. perhaps for Windows 7 it functions, but for Windows XP it doesn't with normal user – kokbira – 2011-05-16T23:41:41.427