如何在批处理文件中获取一年中的某一天

时间:2015-07-22 13:50:55

标签: windows date batch-file

如何从Windows批处理文件中获取当前日期的某一天?

我试过了

SET /A dayofyear=(%Date:~0,2%*30.5)+%Date:~3,2%

但它不适用于闰年,它总是在几天之后关闭。我不想使用任何第三方可执行文件。

3 个答案:

答案 0 :(得分:1)

如果您想要朱利安日编号,您可以使用上一个链接中给出的my accepted answer中发布的方法。然而,一年中的一天"只是1到365之间的数字(闰年为366)。下面的批处理文件正确计算它:

@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
)

set /A month=1%Date:~0,2%-100, day=1%Date:~3,2%-100, yearMOD4=%Date:~6,4% %% 4
set /A dayOfYear=!accum[%month%]!+day
if %yearMOD4% equ 0 if %month% gtr 2 set /A dayOfYear+=1

echo %dayOfYear%

注意:这取决于日期格式MM/DD/YYYY

答案 1 :(得分:1)

@ Aacini' s answer有两个弱点(虽然它适用于许多应用程序,毕竟这是一个很好的方法!):

  1. 检索到的系统日期依赖于格式MM/DD/YYYY,但%Date%依赖于区域设置;
  2. 计算并不完全符合闰年的定义(见this article);
  3. 以下是@Aacini批量脚本的修订版(在解释性说明中描述):

    @echo off
    setlocal EnableDelayedExpansion
    
    set /A i=0, sum=0
    rem accumulate day-offsets for every month in %accum[1...12]%
    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
    )
    rem check for availability of alternative date value given via (1st) command line argument,
    rem just for convenient batch testing
    if "%1"=="" (
        rem WMIC retrieves current system date/time in standardised format;
        rem parse its output by FOR /F, then store it in %CurrDate%
        for /F "tokens=2 delims==" %%D in ('wmic OS GET LocalDateTime /VALUE ^| find "="') do (
            set CurrDate=%%D
        )
    ) else (
        rem apply date value from command line in YYYYMMDD format (not checked for validity)
        set CurrDate=%1
    )
    rem extract %month% and %day%; 
    set /A month=1%CurrDate:~4,2%-100, day=1%CurrDate:~6,2%-100
    rem compute several moduli needed for determining whether year is leap year
    set /A yearMOD4=%CurrDate:~0,4% %% 4
    set /A yearMOD100=%CurrDate:~0,4% %% 100, yearMOD400=%CurrDate:~0,4% %% 400
    rem calculate %dayOfYear% as it were not a leap year
    set /A dayOfYear=!accum[%month%]!+day
    rem adapt %dayOfYear% only in case %month% is past February (29th)
    if %month% gtr 2 (
        rem check for leap year and adapt %dayOfYear% accordingly
        if %yearMOD4% equ 0 set /A dayOfYear+=1
        if %yearMOD400% neq 0 if %yearMOD100% equ 0 set /A dayOfYear-=1
    )
    rem compound statement to let %dayOfYear% survive SETLOCAL/ENDLOCAL block
    endlocal & set dayOfYear=%dayOfYear%
    rem return result
    echo %dayOfYear%
    

答案 2 :(得分:1)

此操作使用 laps years 在循环中编辑 | for /l %%L in (2020 4 2100) | <或更多!


@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:~-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 "_cnt=0" & 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:-=!

  

| 结果 | Today: 2019_04_21 | Day of Year: 111 | Days Remaining: 254