【问题标题】:problems to format current date (-1month) to YYYYMMDD将当前日期(-1 月)格式化为 YYYYMMDD 的问题
【发布时间】:2016-05-23 19:03:06
【问题描述】:

我正在开发一个批处理脚本,它采用 当前日期 -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%

谢谢

【问题讨论】:

    标签: batch-file batch-processing


    【解决方案1】:

    您的月份比较不正确。从%_test:~4,2% 获取月份意味着当前月份(5 月)返回为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%
    

    【讨论】:

    • 这正是我想要的!谢谢你的解释:)
    • 如果我想在本月 15 日执行此操作呢?
    • @thartaras - 好吧,你最初提供的代码根本不考虑当天的因素,但基于你有一个 _day 变量的事实,我要去勉强说把这个值改为 15。但这只是一个猜测,因为你的代码不完整。
    【解决方案2】:

    无需在%_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%
    

    【讨论】:

      猜你喜欢
      • 2020-03-28
      • 2016-02-21
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2014-07-03
      相关资源
      最近更新 更多