The format of date and time output on running echo %DATE% %TIME%
depends on country set in Windows region settings for the used account, to be more precise date and time format settings derived from configured country customizable individually for each account.
The answer on Why does %date% produce a different result in batch file executed as scheduled task? contains a very detailed explained solution on how to get region independent local date and time.
The batch file below uses this method using command WMIC and changes the time format from 24 to 12 hour format with AM
or PM
.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDateTime=%%I"
set "LocalHour=%LocalDateTime:~8,2%"
set "AM_PM=AM"
if %LocalHour% LSS 12 goto CopyTime
set "AM_PM=PM"
if %LocalHour% GTR 12 set /A LocalHour-=12
set "LocalHour=0%LocalHour%"
:CopyTime
echo %LocalHour:~-2%:%LocalDateTime:~10,2%:%LocalDateTime:~12,2% %AM_PM%| %SystemRoot%\System32\clip.exe
endlocal
The local date/time is assigned in format yyyyMMddHHmmss
to the environment variable LocalDateTime
.
The characters 9 and 10 in this date/time string with character index position 8 and 9 represent the local hour with 00
to 23
assigned to environment variable LocalHour
.
Next the string AM
is assigned as default value to environment variable AM_PM
.
Windows command processor cmd.exe
uses function strtol to convert strings to integers on using set /A
(arithmetic expression) or an IF comparison with the three letter operators like LSS
and GTR
. Function strtol
as used by cmd.exe
interprets strings with a leading 0
as octal numbers which is often a problem, especially in an arithmetic expression because of 08
and 09
are invalid numbers in octal numeral system and for that reason replaced by number 0 on evaluation of an arithmetic expression. But this common problem does not matter in this case because the condition if %LocalHour% LSS 12
is correct true for local hours 00
to 09
without printing an error message to console and correct false for 12
to 23
.
The local current time can be copied to clipboard on being less than 12 as in this case AM
is correct for the time. Otherwise the time is definitely PM
.
For the hours greater than 12 it is necessary to subtract 12 to get the time correct from 12:00:00 PM to 11:23:59 PM.
The arithmetic expression with the subtraction results in having assigned to environment variable the local hour with 1
to 11
which means without leading 0
on hour being less than 10. For that reason the environment variable LocalHour
is redefined with a leading 0
and current value of the hour appended which results in 01
, 02
, ..., 09
, 010
, 011
assigned to environment variable LocalHour
.
The wrong hour format with the three digits is compensated on echo
command line by printing always just the last two characters of string assigned to LocalHour
.
Please note that there is no space between string AM
or PM
and redirection operator |
because a space left to |
would be also output by command echo
as trailing space of the line with current local time in 12 hour format. See also Why does ECHO command print some extra trailing space into the file?