Split

后端 未结 9 1369
感情败类
感情败类 2020-11-29 12:37

Is there a way to split the %date% in a batch file (say, in 3 environment variables), but regardless of Regional Settings? Today\'s date would be 3/13/201

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 13:09

    Four years have passed, but this question doesn't get old, and I think now there's a slightly better answer than using wmic (on win7 onwards).

    for /F "tokens=1,2,3 delims=_" %%i in ('PowerShell -Command "& {Get-Date -format "MM_dd_yyyy"}"') do (
        set MONTH=%%i
        set DAY=%%j
        set YEAR=%%k
    )
    echo %MONTH% %DAY% %YEAR%
    

    With powershell Get-Date you can fine-tune the format you want (short,long, numbers,names,etc..). In this example "MM_dd_yyyy" will get you a numeric date with leading zeros in case of single digit months or days

提交回复
热议问题