Split

后端 未结 9 1367
感情败类
感情败类 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:05

    I found this in http://forums.techguy.org/dos-other/837046-solved-date-format-bat-file.html

    I've reworked it a bit to actually split it in 3 environment variables.

    The downside is it needs to query the registry, probably to find out the order day, month and year.

    @Echo Off
    Set _Date=%date%
    If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
    :: Default Delimiter of TAB and Space are used
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
    IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
    IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
    IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
    Set _Month=%_fdate:~4,2%
    Set _Day=%_fdate:~6,2%
    Set _Year=%_fdate:~0,4%
    Echo _Year=%_Year%
    Echo _Month=%_Month%
    Echo _Day=%_Day%
    

提交回复
热议问题