Jerry Trac
Jerry Trac

Reputation: 367

How to extraxt month from date

I am trying to get the month of today's date (e.g. 08):

set /a m=%date:~4,2% echo m

However, I get the error "Invalid number. constants are either decimal (17) hexadecimal(0x11), or octal(021)". Why? It was working fine until August.

Upvotes: 0

Views: 115

Answers (2)

dbenham
dbenham

Reputation: 130889

Dany Bee is correct - SET /A will attempt to treat 08 as octal and give that error message. You can get the value without the leading 0 by prefixing with a 1 and computing mod 100.

set /a "month=1%date:~4,2% %% 100"

Please be aware that parsing values from the %date% is locale dependent. On my machine I must use set /a "month=1%date:~0,2% %% 100"

Upvotes: 2

Dany Bee
Dany Bee

Reputation: 552

try this:

set "month=%date:~4,2%"
echo %month%

cmd assumes all numbers with leading zeros as octal numbers. "08" is not a valid octal number.

Upvotes: 3

Related Questions