1

I'm fetching the current time from command line using the below command:

echo %time% | clip

Earlier it used to update the time to the clipboard in hh:mm:ss AM/PM 12-hour format, but now for the same command, it is updating the current time in hh:mm:ss.ms 24-hour format with milliseconds followed by a period. Do we have some straight forward command which will update the time to the clipboard in the desired format? Or at least the command to get the time in the original format hh:mm:ss AM/PM?

aschipfl
  • 28,946
  • 10
  • 45
  • 77
  • `Do we have some straight forward command ... to get the time in [a special format]?` No. A short batch script would be necessary (or a Powershell one-liner (recommended)). Or you change your local date-format in the registry (not recommended, as you need to do it on each computer) – Stephan Jul 17 '19 at 14:32
  • Possible duplicate of [How do I get current datetime on the Windows command line, in a suitable format for using in a filename?](https://stackoverflow.com/questions/203090/how-do-i-get-current-datetime-on-the-windows-command-line-in-a-suitable-format) and [Format date and time in a Windows batch script](https://stackoverflow.com/questions/1192476/format-date-and-time-in-a-windows-batch-script) – aschipfl Jul 17 '19 at 16:14

1 Answers1

0

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?

Mofi
  • 38,783
  • 14
  • 62
  • 115