Q:

Windows CMD - How to create time variable with AM PM

How can I create a variable in windows command line (CMD) that uses the following format:
MON-DAY-YEAR_TIME-AM (or PM)
set mon=%DATE:~4,2%
set day=%DATE:~7,2%
set yr=%DATE:~10,4%

> echo %mon%-%day%-%yr%
02-12-2018    # Good so far now I need to add time

Trying to set the output of TIME /T (10:54 AM) to a variable so I can use it with the date above. How can I do this? 
> TIME /T
10:54 AM

Not what im looking for:
>set mytime=TIME /T

>echo %mytime%
TIME /T

A:

Use WMIC OS GET LOCALDATETIME instead:
FOR /F "skip=1" %%A IN ('WMIC OS GET LOCALDATETIME') DO (SET "t=%%A" & GOTO break_1)
:break_1

SET "m=%t:~10,2%" & SET "h=%t:~8,2%" & SET "d=%t:~6,2%" & SET "z=%t:~4,2%" & SET "y=%t:~0,4%"
ECHO %z%-%d%-%y%_%h%:%m%

Output: 02-12-2018_17:27
--------------------------------------------------------------------------------------------------------
If you don't like the 24h format for whatsoever reason, use this:
SETLOCAL EnableDelayedExpansion

FOR /F "skip=1" %%A IN ('WMIC OS GET LOCALDATETIME') DO (SET "t=%%A" & GOTO break_1)
:break_1
SET "m=%t:~10,2%" & SET "h=%t:~8,2%" & SET "d=%t:~6,2%" & SET "z=%t:~4,2%" & SET "y=%t:~0,4%"
IF !h! GTR 11 (SET /A "h-=12" & SET "ap=P" & IF "!h!"=="0" (SET "h=00") ELSE (IF !h! LEQ 9 (SET "h=0!h!"))) ELSE (SET "ap=A")

ECHO %z%-%d%-%y%_%h%:%m%-%ap%M

Output: 02-12-2018_05:27-PM

And if you don't want an additional 0 in front of single digit hours, use this:
FOR /F "skip=1" %%A IN ('WMIC OS GET LOCALDATETIME') DO (SET "t=%%A" & GOTO break_1)
:break_1
SET "m=%t:~10,2%" & SET "h=%t:~8,2%" & SET "d=%t:~6,2%" & SET "z=%t:~4,2%" & SET "y=%t:~0,4%"
IF "%h%"=="00" (SET "h=0") ELSE (IF %h% LEQ 9 SET "h=%h:0=%")
IF %h% GTR 11 (SET /A "h-=12" & SET "ap=P") ELSE (SET "ap=A")

ECHO %z%-%d%-%y%_%h%:%m%-%ap%M

Output: 02-12-2018_5:27-PM

