# Hour in t TIME using batch script

I would lik to get the Hour value alone from a Time stamp ,

TIME = 12:40:48.35 , I would need only the '12' , hw can i do this in a batch script.
###### Who is Participating?

Commented:
EXPLANATION 2:

When the hour is between 1 and 9, the system uses a space followed by a single digit as shown in my previous comment.

If you only want a single digit ('8') instead of two digits ('08') when the hour is between 1 and 9, then you can use the following subtle variant:

hour = %time%              =   8:21:38.91       - get the full system time string
hour = %hour:~0,2%     =   8                      - extract first 2 digits
hour = %hour: =%         = 8                        - strip away leading space

hour = %time%              = 12:21:38.91       - get the full system time string
hour = %hour:~0,2%     = 12                      - extract first 2 digits
hour = %hour: =0%       = 12                      - uneffected
0

Commented:

Left([Time],Instr([time],":")-1)  ' will give you 12 from the sample posted
0

Commented:
In a batch script you would extract those characters with %time:~0,2%.

0

Commented:
Get hour digits from system time:

set hour=%time%
set hour=%hour:~0,2%
set hour=%hour: =0%

(see next comment for expanation)
0

Commented:
EXPLANATION:

hour = %time%              =   8:21:38.91       - get the full system time string
hour = %hour:~0,2%     =   8                      - extract first 2 digits
hour = %hour: =0%       = 08                      - convert spaces to zeros

hour = %time%              = 12:21:38.91       - get the full system time string
hour = %hour:~0,2%     = 12                      - extract first 2 digits
hour = %hour: =0%       = 12                      - convert does not alter result as there are no spaces
0

Author Commented:
ok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.