将当前日期(-1个月)格式化为YYYYMMDD的问题

时间:2016-05-23 19:03:07

标签: batch-file batch-processing

我正在开发一个批处理脚本,其中当前日期为-1个月并将其格式化为 YYYYMMDD ,但是当我执行我的代码时,最终变量只有一年(没有连接月份)。你能帮我找一下这个错误吗?

代码如下:

echo off &setlocal
setlocal EnableDelayedExpansion

SET _test=%date%
SET _month=%_test:~4,2%
SET _day=01
SET search_year=%_test:~10,4%


IF "%_month%" EQU 1 (
        set search_month=12
        set /a search_year= %seach_year% -1
)

IF "%_month%" EQU 2 (
        set search_month=1
)
IF "%_month%" EQU 3 (
        set search_month=2
)

IF "%_month%" EQU 4 (
        set search_month=3
)

IF "%_month%" EQU 5 (
        set search_month=4
)

IF "%_month%" EQU 6 (
        set search_month=5
)

IF "%_month%" EQU 7 (
        set search_month=06
)

IF "%_month%" EQU 8 (
        set search_month=07
)

IF "%_month%" EQU 9 (
        set search_month=08
)

IF "%_month%" EQU 10 (
        set search_month=09
)

IF "%_month%" EQU 11 (
        set search_month=10
)

IF "%_month%" EQU 12 (
        set search_month=11
)


echo Month-1 is %search_month%
set /a total = %search_year%%search_month%
echo total is %total%

由于

2 个答案:

答案 0 :(得分:0)

您的月份比较不正确。从%_test:~4,2%获取月份意味着当前月份(五月)将返回05,而不是5

此外,当比较的一侧有引号时,另一方必须有引号,因为批处理在比较时包含引号。由于这两件事,search_month永远不会被设置。您的比较应如下所示:

IF "%_month%" EQU "05" (
    set search_month=4
)

也就是说,12个基本相同的比较是大量的冗余代码,你可以简化:

@echo off
setlocal enabledelayedexpansion

SET _test=%date%
SET _month=%_test:~4,2%
SET _day=01
SET search_year=%_test:~10,4%

if "%_month%" equ "01" (
    set search_month=12
    set /a search_year-=1
) else (
    REM Remove any zero-padding so that months are not considered octal.
    REM We do this by putting a 1 in front of %_month% and then subtracting 100
    REM because set /a returns in decimal.
    REM And then we subtract 1 because we're getting the previous month.
    set /a search_month=1%_month%-100-1

    REM Replace the zero-padding since you want YYYYMM format.
    REM Use delayed expansion here because search_month was created inside
    REM of a code block.
    if !search_month! lss 10 set search_month=0!search_month!
)

echo Month-1 is !search_month!
set total=%search_year%!search_month!
echo total is %total%

答案 1 :(得分:0)

无需在%_month%周围加上引号 这就是你想要的,

echo off &setlocal
setlocal EnableDelayedExpansion

SET _test=%date%
SET _month=%_test:~4,2%
SET _day=01
SET search_year=%_test:~10,4%


IF %_month% EQU 1 (
        set search_month=12
        set /a search_year= %seach_year% -1
)

IF %_month% EQU 2 (
        set search_month=01
)
IF %_month% EQU 3 (
        set search_month=02
)

IF %_month% EQU 4 (
        set search_month=03
)

IF %_month% EQU 5 (
        set search_month=04
)

IF %_month% EQU 6 (
        set search_month=05
)

IF %_month% EQU 7 (
        set search_month=06
)

IF %_month% EQU 8 (
        set search_month=07
)

IF %_month% EQU 9 (
        set search_month=08
)

IF %_month% EQU 10 (
        set search_month=09
)

IF %_month% EQU 11 (
        set search_month=10
)

IF %_month% EQU 12 (
        set search_month=11
)


echo Month-1 is %search_month%
set /a total = %search_year%%search_month%
echo total is %total%
相关问题