Ayusman
Ayusman

Reputation: 8719

Windows batch file timestamp to string

I am using the following simple line in my windows batch file to get the current time stamp to a String format so that I can use it later in the batch file to create a folder with same name.

set TIME_STAMP=%DATE:/=-%_%TIME::=-%

I observed that when the time is single digits, say 9:31 AM, I get the String like this:

08-10-2015_ 9.31.52.57

Notice the space between the characters _ and 9. When the system time is say 10:31 AM, it all works fine, like

08-10-2015_10.31.52.57

Is there something I can do to make the time stamp as

08-10-2015_09.31.52.57

when I have hours in single digits?

Upvotes: 1

Views: 699

Answers (2)

JosefZ
JosefZ

Reputation: 30113

Probably the simpliest approach:

set "TIME_STAMP=%DATE:/=-%_%TIME::=-%"
set "TIME_STAMP=%TIME_STAMP: =0%"

Result:

==> echo "%TIME_STAMP%"
"08.10.2015_07-42-08,18"

==>

Upvotes: 0

Paul
Paul

Reputation: 2710

just do this

set TIME_STAMP=%DATE:/=-%_%TIME::=-%
echo %TIME_STAMP: =0%

Upvotes: 1

Related Questions