我尝试在 cmd 窗口中格式化日期,但结果并不好。
echo %date% +"%d-%B-%Y"
。我该怎么做呢?
答案1
您可能对 wmic 命令感兴趣,它可以以与语言环境无关的方式返回本地日期/时间。
@echo off
Title wmic command which can return the local date/time in a locale-independent manner
@for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
echo Today : %today%
set "year=%MyDate:~2,2%"
set "month=%MyDate:~4,2%"
set "day=%MyDate:~6,2%"
echo %day%-%month%-%year%
pause
答案2
我相信这就是你想要的:
set year=%date:~10,4%
set month=%date:~4,2%
set day=%date:~7,2%
set dateformatted=%month%-%day%-%year%
echo %dateformatted%
来自的回答这里。 玩得开心!