批处理脚本错误:获取一年中的哪一天

Posted

技术标签:

【中文标题】批处理脚本错误:获取一年中的哪一天【英文标题】:batch script Errror : get the day of the year 【发布时间】:2020-11-29 18:50:45 【问题描述】:

这个脚本让我从 Windows 批处理文件中的当前日期获取一年中的哪一天

我从一月份开始使用它,没有问题,但这个月脚本突然中断并显示不正确的信息。

这是错误:

Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11) or octal (021) numbers.

这里是批处理代码

 @echo off & setlocal enabledelayedexpansion

set "_cmd=Get Day^,Month^,Year^"
for /l %%L in (2020 4 2100)do set "_array_leap_year_=!_array_leap_year_!%%L,"


for /f "tokens=1-3delims= " %%a in ('wmic Path Win32_LocalTime !_cmd! ^| findstr /r "[0-9]"')do (
    set "_yy=%%c"
    set "_mm=0%%b"
    set "_dd=0%%a"
    set "_mm=!_mm:~-2!"
    set "_dd=!_dd:~-2!"
    set _date=!_yy!_!_mm!_!_dd!
    )

    echo/!_array_leap_year_!|findstr /lic:"!_date:~0,4!," >nul && (
    set "_leap_=29" & set "_year_=366" )||( set "_leap_=28" & set "_year_=365" )

    set "_mm_dd_year_=01-31,02-!_leap_!,03-31,04-30,05-31,06-30,07-31,08-31,09-30,10-31,11-30,12-31"
    set /a "_loop=!_mm! * 6"

    for /l %%d in (0 6 !_loop!)do set "_sum=!_mm_dd_year_:~%%d,5!" && (
    if "9!_sum:~,2!" lss "9!_mm!" set /a "_day_year_+=!_sum:~-2!"  )

    set /a "_day_year_+=!_dd!"
    set /a "_remain=!_day_year_! - !_year_!"
    echo/Today: !_date! ^| Day of Year: !_day_year_! ^| Days Remaining: !_remain:-=!

【问题讨论】:

输入set /? 并查看set /A 部分;你会发现前导数字标记的是八进制数字,所以 0809 是无效数字然后...... 这能回答你的问题吗? Invalid number error when setting variable 要直接解决您的问题,请执行set /a "_loop=1!_mm! * 6 - 600" @Gerhard,它不起作用,它显示一年中的第 274 天,但今天是:第 223 天 我刚刚运行了脚本,改变了行并得到Today: 2020_08_10 | Day of Year: 223 | Days Remaining: 143 【参考方案1】:

原样复制代码:

我们在这里更改的只是set /a "_loop=1!_mm! * 6 - 600",它有效地执行set _loop=108 * 6 - 600,导致486 * 8的结果相同

@echo off & setlocal enabledelayedexpansion
set "_cmd=Get Day^,Month^,Year^"
for /l %%L in (2020 4 2100)do set "_array_leap_year_=!_array_leap_year_!%%L,"

for /f "tokens=1-3delims= " %%a in ('wmic Path Win32_LocalTime !_cmd! ^| findstr /r "[0-9]"')do (
    set "_yy=%%c"
    set "_mm=0%%b"
    set "_dd=0%%a"
    set "_mm=!_mm:~-2!"
    set "_dd=!_dd:~-2!"
    set _date=!_yy!_!_mm!_!_dd!
    )

    echo/!_array_leap_year_!|findstr /lic:"!_date:~0,4!," >nul && (
    set "_leap_=29" & set "_year_=366" )||( set "_leap_=28" & set "_year_=365" )

    set "_mm_dd_year_=01-31,02-!_leap_!,03-31,04-30,05-31,06-30,07-31,08-31,09-30,10-31,11-30,12-31"
    set /a "_loop=1!_mm! * 6 - 600"

    for /l %%d in (0 6 !_loop!)do set "_sum=!_mm_dd_year_:~%%d,5!" && (
    if "9!_sum:~,2!" lss "9!_mm!" set /a "_day_year_+=!_sum:~-2!"  )

    set /a "_day_year_+=!_dd!"
    set /a "_remain=!_day_year_! - !_year_!"
    echo/Today: !_date! ^| Day of Year: !_day_year_! ^| Days Remaining: !_remain:-=!

【讨论】:

【参考方案2】:

您可以改用this simpler method:

@echo off
setlocal EnableDelayedExpansion

set /A i=0, sum=0
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
   set /A i+=1
   set /A accum[!i!]=sum, sum+=%%a
)

for /F "tokens=1-3" %%a in ('wmic Path Win32_LocalTime Get Day^,Month^,Year') do (
   set /A day=%%a, month=%%b, yearMOD4=%%c %% 4 2>NUL
)
set /A dayOfYear=accum[%month%]+day
if %yearMOD4% equ 0 if %month% gtr 2 set /A dayOfYear+=1

echo %dayOfYear%

编辑添加了更简单的方法

这个方法可以这样简化和缩短:

@echo off
setlocal

set "daysPerMonth=0 31 28 31 30 31 30 31 31 30 31 30"

for /F "tokens=1-3" %%a in ('wmic Path Win32_LocalTime Get Day^,Month^,Year') do (
   set /A "dayOfYear=%%a, month=%%b, leap=!(%%c%%4)*(((month-3)>>31)+1)" 2>NUL
)
set /A "i=1, dayOfYear+=%daysPerMonth: =+(((month-(i+=1))>>31)+1)*%+leap"

echo %dayOfYear%

【讨论】:

【参考方案3】:

当然,您也可以使用batch-file 中的powershell:

@For /F %%G In ('%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command ^
 "$NOW=Get-Date;$DOY=$NOW.DayOfYear;$YDR=(Get-Date -Month 12 -Day 31).DayOfYear-$DOY;" ^
 "Write-Host ""_date=$($NOW.ToString('yyyy_MM_dd'))`n_day_year_=$DOY`n_remain=$YDR"""'
)Do @Set "%%G"
@Echo Today: %_date% ^| Day of Year: %_day_year_% ^| Days Remaining: %_remain% & Pause

【讨论】:

以上是关于批处理脚本错误:获取一年中的哪一天的主要内容,如果未能解决你的问题,请参考以下文章

查找一周中的哪一天

Python在轴上将一年中的日期转换为月份

批处理 + 文件 + 一年中的某一天

带有自定义日期的 UIPickerView

从批处理脚本启动exe并在缺少dll时获取错误代码

在 r 中创建数据子集