You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Windows批处理日期变量格式以去除日/月前导零?

Fixing Leading Zeros in Batch Date Formatting

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 version2 string 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:12:19