0

I need to get the current time formatted as HHMMSS, with a leading space changed to a leading zero.

Is a one-liner possible that both replaces a leading space with zero AND extracts just the digits from hh:mm:ss?

The ultimate goal I am seeking is to have a ONE LINE statement to create a variable formatted as HHMMSS

It is trivial to do this in two or more lines, but if substring replacement and substring extraction can be used in the same line, it would be great.

Focusing only on the current hour, I have tried things like..

%time:~0,2: =0%
%time:0,2$ =0%
%%time:~0,2%: =0%
(etc.)

Of course none of these succeeded.

The following is my original one-liner, and it works except for the leading space for the hours 0-9

set NOW=%time:~0,2%%time:~3,2%%time:~6,2%

I realize this has been previously marked as a duplicate of Variables are not behaving as expected but after having read that answer I cannot figure out how it solves my issue.

It may in some respect be a duplicate, but after reading it twice I haven't been able to make the step from that answer to my question.

One key aspect of my question is I am looking for a one-liner.

Calling a multi-line function is not a one-liner if you consider the multi-line function as part of the solution.

Paperclip Bob
  • 346
  • 2
  • 8
  • personally, I'd use something like `call :formattime` with a multi-line subroutine like `set now=%time: =0` etc. – Stephan Aug 11 '19 at 07:27
  • 2
    `set NOW=%time:~0,2%%time:~3,2%%time:~6,2%&CALL SET "now=%%now: =0%%"` would be the standard approach, if you require a one-liner. After `now` is set to the current time sans punctuation, use a subshell to replace the spaces with zeroes. – Magoo Aug 11 '19 at 07:51
  • 1
    Same result with `set now=%time:~0,8%&call set now=%%now: =0%%&call set now=%%now::=%%` – Stephan Aug 11 '19 at 07:58
  • 1
    `Calling a multi-line function is not a one-liner` no, it's not, but it keeps your main code clean and readable ("One-Liner" in main code). That's the purpose of a subroutine. In case of troubleshooting, I'd always prefer a readable code (including a readable subroutine) instead of something clutched as the two solutions above. Hey - it's a batch file, the number of lines isn't a thing (except you do [code golfing](https://codegolf.stackexchange.com/)). – Stephan Aug 11 '19 at 08:13

0 Answers0