Get date and time on the same line
I am no wizard with cmd.exe, but this works. There may be an easier way!
@echo off
setlocal enableextensions
for /f "tokens=*" %%a in ('date /t') do (
set d=%%a
)
for /f "tokens=*" %%a in ('time /t') do (
set t=%%a
)
REM Add those puppies together
set "dt=%d% %t%"
echo %dt%
27/11/2013 16:04
Try the following (PowerShell):
Get-Date -Format G
27.11.2013 17:10:23
The format is defined with the system's regional settings, so for me this is what I get, but if you're regional date/time format is what you want, it should show up like you want.
(Get-Date).ToString()
would probably also work.
UPDATE:
"Date and time is: $((Get-Date).ToString())"
In PowerShell this is trivial:
(Get-Date).ToString('MM/dd/yyyy hh:mm:ss tt')
In cmd
it's somewhat complicated:
rem Get the date and time in a locale-agnostic way
for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
rem Leading zeroes for everything that could be only one digit
set Month=0%Month%
set Day=0%Day%
rem Hours need special attention if one wants 12-hour time (who wants that?)
if %Hour% GEQ 12 (set AMPM=PM) else (set AMPM=AM)
set /a Hour=Hour %% 12
if %Hour%==0 (set Hour=12)
set Hour=0%Hour%
set Minute=0%Minute%
set Second=0%Second%
set Month=%Month:~-2%
set Day=%Day:~-2%
set Hour=%Hour:~-2%
set Minute=%Minute:~-2%
set Second=%Second:~-2%
rem Now you can just create your output string
echo %Month%/%Day%/%Year% %Hour%:%Minute%:%Second% %AMPM%
Note that lots of code is wasted on supporting that weird date and time format. And leading zeroes if necessary. Note also that this code works regardless of the user's regional and language settings. It does not depend on the user's date format, for example (which for me is ISO 8601 anyway).