0

I want to make a clock in batch, but when typed echo %time% the decimal seconds are printed as well.
which will output:

15:18:42,15

so i need to get rid of that ,15 but how?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sam
  • 381
  • 3
  • 15
  • Possible duplicate of [Format date and time in a Windows batch script](https://stackoverflow.com/questions/1192476/format-date-and-time-in-a-windows-batch-script) – jwdonahue Jan 17 '18 at 23:39

2 Answers2

3

you can use substing manipulation, as described in set /?:

echo %time:~0,8%

takes 8 characters, beginning at the first character (zero-based counting)

Stephan
  • 53,940
  • 10
  • 58
  • 91
2

Here is an alternative way:

for /F "delims=,." %%T in ("%TIME%") do @echo/%%T

The for /F loop iterates once over the time string and splits off the first , or . and everything after.


Anyway, regard that %TIME% (as well as %DATE%) returns a locale-dependent time (or date) string.

To get the current time in a locale-independent manner, use the wmic command:

wmic OS get LocalDateTime

This returns something like:

20180117151842.150000+000

Now let us capture this using for /F and build our own time string, using sub-string expansion:

@echo off
for /F %%D in ('wmic OS get LocalDateTime') do set "DATETIME=%%D"
echo %DATETIME:~8,2%:%DATETIME:~10,2%:%DATETIME:~12,2%

And this always returns, independent on the locale and region settings, something like this:

15:18:42
aschipfl
  • 33,626
  • 12
  • 54
  • 99