Trimming milliseconds from time value in command line
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
windows-7 command-line
add a comment |
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
windows-7 command-line
add a comment |
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
windows-7 command-line
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
windows-7 command-line
windows-7 command-line
edited Mar 4 '12 at 14:59
Der Hochstapler
68.3k50230286
68.3k50230286
asked Mar 4 '12 at 14:36
TedTed
14516
14516
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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%
add a comment |
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
add a comment |
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
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f396783%2ftrimming-milliseconds-from-time-value-in-command-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%
add a comment |
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%
add a comment |
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%
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%
edited Mar 4 '12 at 14:57
answered Mar 4 '12 at 14:47
Der HochstaplerDer Hochstapler
68.3k50230286
68.3k50230286
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Mar 4 '12 at 15:12
answered Mar 4 '12 at 14:45
BobBob
46.3k20141173
46.3k20141173
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Feb 17 at 13:21
HayzHayz
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f396783%2ftrimming-milliseconds-from-time-value-in-command-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown