I would like to have this run and show the previous month. When I try to subtract the month it makes the last day of the month field not appear.
@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%
Dates are complicated to work with and easy to get wrong, and if you can avoid rolling your own, do so.
CMD does not come with a native date library, but the .NET
Get-LastMonthStats.ps1System.DateTime
library is available via PowerShell. The following PS script shows how to use .NET to do what you're asking.Nearly all of this script is formatting the output. To run from CMD, launch this with the command
Alternatively, you can put the whole script on one (very long) command line, if you don't want to create a separate
.ps1
file.here's a piece of code that will get the previous month:
Better use wmic to get the date parts because it's independent from the machine's date format unlike the
%date%
variable.EDIT (as requested in the comments) :
I made a function library that has these functions. Doing what you want with this library is easy, check below.
The functions are at the end of the code. Just copy to the end of your file (after a goto:eof) and call them using specified arguments.
The given code inside :main should do what you are asking for.
Remarks:
Code: