• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3178
  • Last Modified:

DateAdd for Windows Command Prompt?

I can get today's date in the format I need it, however now I need to subtract/add a month to it.

Can anyone help?


@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)
 
@echo %Month%/%Day%/%Year%

Open in new window

0
kevp75
Asked:
kevp75
  • 2
3 Solutions
 
Steve KnightIT ConsultancyCommented:
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%B
    Set Day=%%A
    Set Year=%%C
)

Well.... taking just a month off isn't TOO hard using set /a.  

set /a lastmonth=%month%-1
set lastyear=%year%
if %lastmonth%==0 set lastmonth=12 & set lastyear=%year%-1
echo last month was %lastmonth%/%lastyear%

and then you just need to deal with whether it is January and turn the year back.

hth

Steve
0
 
Steve KnightIT ConsultancyCommented:
(I had to change dd and mm around as I use dd/mm/yyyy not mm/dd/yyyy in the for command).  If your month shows as 01 02 etc. rather than 1 2 etc. then we need to take that into account, also a typo error there above on testing with Jan date:

set month=01
set year=2009
set /a lastmonth=1%month%-101
set lastyear=%year%
if %lastmonth%==0 set lastmonth=12& set /a lastyear=%year%-1
echo last month was %lastmonth%/%lastyear%
0
 
AmazingTechCommented:
I assume you're using preceeding 0 as in 02 for February. If you do you'll need to make the /a keep the 0 and also be able to do arithmetic functions for 08 and 09.
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)
 
Set LastYear=%Year%
Set /a LastMonth=1%Month% - 1
if %LastMonth% EQU 100 Set /a LastYear-=1 & Set LastMonth=112
 
@echo Today: %Month%/%Day%/%Year%
@echo Last month: %LastMonth:~1%/%Day%/%LastYear%

Open in new window

0
 
kevp75Author Commented:
thanks folks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now