在Windows批处理中减去月份

时间:2016-05-11 18:14:47

标签: windows batch-file cmd

我希望这次运行并显示上个月。当我尝试减去月份时,它会显示月份的最后一天字段。

@echo off

set FirstDay=01

set Month=%date:~4,2% 

set Year=%date:~10,4%



if %Month%==01 set LastDay=31 & goto foundate
if %Month%==02 set LastDay=28 & goto foundate
if %Month%==03 set LastDay=31 & goto foundate
if %Month%==04 set LastDay=30 & goto foundate
if %Month%==05 set LastDay=31 & goto foundate
if %Month%==06 set LastDay=30 & goto foundate
if %Month%==07 set LastDay=31 & goto foundate
if %Month%==08 set LastDay=31 & goto foundate
if %Month%==09 set LastDay=30 & goto foundate
if %Month%==10 set LastDay=31 & goto foundate
if %Month%==11 set LastDay=30 & goto foundate
if %Month%==12 set LastDay=31 & goto foundate



:foundate
echo The year is: %Year%
echo The month is: %Month%
echo First day of this month is: %FirstDay%
echo Last day of this month is: %LastDay%

4 个答案:

答案 0 :(得分:0)

这是上个月会得到的一段代码:

@echo off

for /f "" %%# in ('WMIC Path Win32_LocalTime Get month /format:value') do (
    for /f %%Z in ("%%#") do set /a %%Z-1
)
if %month% equ 0 set month=12
echo %month%

更好地使用wmic来获取日期部分,因为它与机器的日期格式无关,与%date%变量不同。

编辑(根据评论中的要求):

@echo off


setlocal disableDelayedExpansion
for /f "" %%# in ('"WMIC Path Win32_LocalTime Get month,year /format:value"') do (
    for /f %%Z in ("%%#") do set "%%Z"
)
set /a month=month-1
if %month% equ 0 set month=12
rem echo %month% -- %year%
set /A "leap=!(year&3) + (!!(year%%100)-!!(year&15))"
if %leap% equ 0 (set "m2=29") else (set "m2=28")

setlocal enableDelayedExpansion
set m1=31
set m3=31
set m4=30
set m5=31
set m6=30
set m7=31
set m8=31
set m9=30
set m10=31
set m11=30
set m12=31

set premonLastDay=!m%month%!




endlocal & set premonLastDay=%premonLastDay%
if %month% equ 12 (

        set /a year=year-1

)

echo %premonLastDay%-%month%-%year%

endlocal

答案 1 :(得分:0)

我创建了一个具有这些功能的函数库。您可以使用此库轻松完成所需操作,请查看以下内容。

这些功能位于代码的末尾。只需复制到文件的末尾(在goto:eof之后)并使用指定的参数调用它们。

里面给出的代码:main应该做你想要的。

<强>说明:

  • 内置闰年检查,所以如果这一年是闰年,Februrary将返回29天。
  • 将功能复制到您的文件时,请保留作者信息

<强>代码:

@echo off
:main
  :: Recommended to use GetDateIntl function, to get region-independent results (Check functions at the end of this file)
  call :GetDateIntl Year Month Day /A
  echo/Using GetDateIntl to get current date: %Year%-%Month%-%day%
  echo/

  :: Call function to get last day of this month (Check functions at the end of this file)
  call :GetLastDayOfMonth %Month% %Year% LastDay
  echo This month is: %Month%/%Year%
  echo First day of any month is always 1
  echo Last day of this month is: %LastDay%
  echo/

  :: Get previous month number
  set /A "PMonth= %Month% - 1"
  set "PYear=%Year%"
  :: Correct PMonth and PYear if this month is December
  if %PMonth%==0 ( set "PMonth=12" & set /A "PYear= %PYear% - 1" )

  :: Call function to get last day of previous month (Check function after :main)
  call :GetLastDayOfMonth %PMonth% %PYear% PLastDay
  echo Previous month is: %PMonth%/%PYear%
  echo Last day of previous month is: %PLastDay%

goto:eof


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDateIntl yy mm dd [/A]
:: Returns the current date on any machine with regional-independent settings
:: Arguments:
::   yy = variable name for the year output
::   mm = variable name for the month output
::   dd = variable name for the day output
::   /A = OPTIONAL, removes leading 0 on days/months smaller than 10
::
:: By Cyberponk,    v1.1 - 11/05/2016
::          v1.0 - 2015
::
SETLOCAL ENABLEEXTENSIONS
if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
  for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
    set '%%a'=%%i
    set '%%b'=%%j
    set '%%c'=%%k
  )
)
if "%'yy'%"=="" set 'yy'=%'aa'%
if %'yy'% LSS 100 set 'yy'=20%'yy'%
endlocal&set %1=%'yy'%&set %4 %2=%'mm'%&set %4 %3=%'dd'%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsLeapYear %year% IsLeap
:: Checks if a year is a Leap Year (year that has 366 days)
:: Arguments:
::   %year% = year number input. Can be a number or a variable containing a 4 digit number
::   IsLeap = result output containing 1 or 0 (1= is leap year, 0= not yeap lear)
::
:: By Cyberponk,    v1.1  - 11/05/2016
::
SETLOCAL ENABLEEXTENSIONS
  set /A "IsLeap=(!(%1 %% 4) & !!(%1 %% 100)) | !(%1 %% 400)"
endlocal & set %2=%IsLeap%& goto:EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetLastDayOfMonth %month% %year% LastDay
:: Gets the last day of a given month from a given year
:: Arguments:
::   %month% = month number input. Can be a number or a variable containing a number
::   %year%  = year for reference (used to check for leap years)
::   LastDay = result output containing last day of the specified month
::
:: By Cyberponk,    v1.0  - 11/05/2016
::
SETLOCAL ENABLEEXTENSIONS
  :: Mathematical formula to last day of month
  set /A "LastDay= 28 + (%1 + (%1/8)) %% 2 + 2 %% %1 + 2 * (1/%1)

  :: Add 1 day if year is a Leap Year and month is February
  call :IsLeapYear %2 IsLeap  
  if %LastDay%==28 set /A LastDay=%LastDay% + %IsLeap%
endlocal & set %3=%LastDay%& goto:EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

答案 2 :(得分:0)

日期很复杂,容易出错,如果你可以避免自己滚动,那就这样做。

CMD没有本机日期库,但.NET System.DateTime库可通过PowerShell获得。以下PS脚本显示了如何使用.NET来执行您所要求的操作。

GET-LastMonthStats.ps1
$lastmonth = (Get-Date).addMonths(-1)

"The year is $($lastmonth.year)"
"The month is $($lastmonth.Month)"
"First day of this month is 01"
"Last day of this month is $([DateTime]::DaysInMonth($lastmonth.year, $lastmonth.month))"

几乎所有这个脚本都在格式化输出。要从CMD运行,请使用命令

启动它
powershell.exe -ex bypass -f Get-LastMonthStats.ps1

或者,如果您不想创建单独的.ps1文件,则可以将整个脚本放在一个(非常长的)命令行上。

powershell -c "$lastmonth = (Get-Date).addMonths(-1); 'The year is ' + $lastmonth.year; 'The month is ' + $lastmonth.Month; 'First day of this month is 01'; 'Last day of this month is ' + [DateTime]::DaysInMonth($lastmonth.year, $lastmonth.month)"

答案 3 :(得分:0)

我已针对以下错误修改了Cyber​​ponk脚本
上个月是:-1/2020

之后

if "%'yy'%"=="" set 'yy'=%'aa'%

我已添加为修复程序

if %'mm'% LSS 10 set 'mm'=%'mm':~1,1%