Skip to content

Instantly share code, notes, and snippets.

@RichardHan
Created July 27, 2020 02:57
Show Gist options
  • Save RichardHan/89dfb991927a8d5a18f515493e0f9385 to your computer and use it in GitHub Desktop.
Save RichardHan/89dfb991927a8d5a18f515493e0f9385 to your computer and use it in GitHub Desktop.
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%
echo =========================
echo OR
echo =========================
@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%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment