根据sysdate将月份和期间分配给变量

时间:2016-07-28 21:38:11

标签: windows batch-file cmd

原始问题: 我试图将月份和年份值分配给从4月到3月的18个月的财年变量。我期待批次选择sys月份和年份并分配18个月。月份变量应以月份值递增至18个月,年份变量值应从4月底开始递增。

1 个答案:

答案 0 :(得分:0)

使用少量模数学并将索引编入月份名称缩写列表并不太难。我离开了机制来获取CUR_MONTH和CUR_YEAR。你可以解析%DATE%,但那会很差。寻找通过WMIC获取当前时间戳的方法。

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

REM Use 0-based month index.
REM Jan = 0, Feb = 1, ...

FOR /F "usebackq skip=1" %%a IN (`type NUL ^|WMIC Path Win32_LocalTime Get Month ^| findstr .`) DO (SET /A "CUR_MONTH=%%a - 1")
FOR /F "usebackq skip=1" %%a IN (`type NUL ^|WMIC Path Win32_LocalTime Get Year ^| findstr .`) DO (SET /A "CUR_YEAR=%%a")

SET "MONTH_LIST=JanFebMarAprMayJunJulAugSepOctNovDec"
SET /A "FISCAL_YEAR=%CUR_YEAR%"

FOR /L %%i IN (0, 1, 17) DO (
    SET /A "MONTH_INDEX=(!CUR_MONTH! + %%i) %% 12"
    SET /A "MONTH_OFFSET=!MONTH_INDEX! * 3"

    SET /A "VAR_INDEX=%%i + 1"
    CALL SET "MONTH!VAR_INDEX!=%%MONTH_LIST:~!MONTH_OFFSET!,3%%"

    IF !MONTH_INDEX! EQU 3 (SET /A "FISCAL_YEAR=!FISCAL_YEAR! + 1")
    SET "YEAR!VAR_INDEX!=FY!FISCAL_YEAR:~2,2!"
)
SET "MONTH_LIST="
SET "MONTH_INDEX="
SET "MONTH_OFFSET="

SET MONTH
SET YEAR

EXIT /B 0

输出是:

MONTH1=Aug
MONTH10=May
MONTH11=Jun
MONTH12=Jul
MONTH13=Aug
MONTH14=Sep
MONTH15=Oct
MONTH16=Nov
MONTH17=Dec
MONTH18=Jan
MONTH2=Sep
MONTH3=Oct
MONTH4=Nov
MONTH5=Dec
MONTH6=Jan
MONTH7=Feb
MONTH8=Mar
MONTH9=Apr
YEAR1=FY17
YEAR10=FY17
YEAR11=FY17
YEAR12=FY17
YEAR13=FY18
YEAR14=FY18
YEAR15=FY18
YEAR16=FY18
YEAR17=FY18
YEAR18=FY18
YEAR2=FY17
YEAR3=FY17
YEAR4=FY17
YEAR5=FY17
YEAR6=FY17
YEAR7=FY17
YEAR8=FY17
YEAR9=FY17