如何修改Windows批处理日期变量格式以去除日/月前导零?
Got it, let's tackle that leading zero problem in your version string! Your current command pulls the month and day with their leading zeros, but we can adjust that with some simple batch variable checks.
Here's the modified approach
First, we'll break out the date components into separate variables, then strip any leading zeros from the month and day before building your final version2 string:
@echo off :: Extract individual date components (matches your original DATE string structure: dd/mm/yy) set "year=%DATE:~8,2%" set "month=%DATE:~3,2%" set "day=%DATE:~0,2%" :: Remove leading zero from month if present if "%month:~0,1%" equ "0" set "month=%month:~1,1%" :: Remove leading zero from day if present if "%day:~0,1%" equ "0" set "day=%day:~1,1%" :: Build your version string without leading zeros set "version2= %11.%year%.%month%.%day%"
How this works
- We first isolate the year, month, and day into their own variables for easier manipulation.
- For both month and day, we check if the first character is a
0. If it is, we overwrite the variable with just the second character (dropping the leading zero). - Finally, we assemble the
version2string using the cleaned-up month and day values.
Quick note
Keep in mind that the %DATE% variable's format depends on your system's regional settings. Your original command assumes a dd/mm/yyyy or dd/mm/yy format—if your system uses a different date format (like mm/dd/yyyy), you'll need to adjust the substring positions (~3,2, ~0,2, etc.) to match.
内容的提问来源于stack exchange,提问作者Hendouz

