I'm writing a script to RAR-compress my files and name the result with the time stamp.

I use the TIME environment variable. I also do

set _time=%TIME::=.%` 

to use only allowed characters in the filename.

How do I get rid of the milliseconds part of the %TIME%, e.g. 16:35:42.07

3 Answers 3


Just cut off the characters you don't need afterwards:

 set _time=%_time:~0,-3%

Or get the full, desired result directly like this:

set _time=%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%

The easy way would be if the milliseconds field is always two digits: %_TIME:~0,-3%
Translates to trim off the first 0 characters, then keep up to (size of string - 3) characters. Which would lop off the .07 in your example. In fact, this should always work: I just realised you're using the built in %time% variable (teaches me to read), which should always return with a two digit 'ms' value.

If your milliseconds could be a different number of digits (e.g. 0.1 instead of 0.10), some kind of string search command would be needed. And here it is:

Ok, this seems hackish.. but it's Windows command line, so hey. This will trim off the dot and everything after it, so it will work no matter how many digits of precision your ms value is (e.g. 0.1 instead of 0.10).

SetLocal EnableDelayedExpansion

set _time=%time%_EndOfStringMarker
set __DotToEnd=%_time:*.=%
set _time=!_time:%__DotToEnd%=!
set _time=%_time:.=%

EndLocal && set _time=%_time%

Make sure there is only one dot in your time when this is used.

Attempting to explain now. Of course, you can just use it and save yourself the headache of trying to understand.

  • SetLocal defines a local scope. Anything changed between now and the EndLocal will not last past the EndLocal. You cannot have a SetLocal within a SetLocal.
  • EnableDelayedExpansion allows the use of !s, among other things. It lets me use a variable within a variable.
  • I add an _EndOfStringMarker so if the ms value (in your example 07) occurs anywhere else, e.g. the seconds value, that will not be replaced.
  • I trim off from before the dot, including the dot, leaving me with everything after the dot. The command line doesn't let me do it the other way around.
  • I replace everything after the dot in the original string with nothing, leaving me with the time ending in a dot.
  • I replace the dot with nothing.
  • I end the local area, to tidy things up a bit. To keep the new time value outside this local scope, I have to set it to itself on the same line. It's yet another quirk of the command line.

Source for the dodgy trim to end: http://ss64.com/nt/syntax-replace.html


You can use variable syntax:

@echo off
set hrs=%time:~0,2%
set mns=%time:~3,2%
set scs=%time:~6,2%
set mls=%time:~9,2%
set nmt=%time:~0,8%

Use %hrs% for the hours

Use %mns% for the minutes

Use %scs% for the seconds

Use %mls% for the miliseconds

Use %nmt% for the time without the miliseconds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.